STRUTS2 series: (7) wildcard characters and dynamic method calls

Source: Internet
Author: User


Current problem: In the Struts.xml configuration file, each action tag corresponds to a method in a class. However, in the actual Javaweb project development, there are many classes that inherit from the Actionsupport class (which also includes many methods), and if each method corresponds to an action tag, it will cause struts.xml to be very large.


The wildcard characters and dynamic method calls described in this section are intended to address this problem. This section is divided into 2 parts:(1) wildcard mapping and (2) dynamic invocation


concept :struts applications may have many action declarations that simplify multiple similar mappings into a single (common) mapping relationship (multiple action classes----an action tag)


1. Wildcard Mapping rules

Generally use "*" to match 0 or more characters

The substring of the URI string that is matched by the wildcard character can be referenced by {1}, {2}. {1} matches the first substring, {2} matches the second substring ... N


(1) Exact match priority. If more than one match is found, the one that does not have a wildcard will be used.

(2) If the specified action does not exist, Struts will attempt to match the URI with any action name containing the wildcard character *.

(3) If there are multiple matches with wildcard characters, the first match shall prevail. So pay attention to the order of the action with wildcards .


Mapping Case 1

<action name= "*" class= "Com.rk.strut.d_wildcard. {1} "method=" Add "> <result name=" Add ">/wildcard/{1}.jsp</result> </action>

Mapping Case 2

<action name= "user_*" class= "Com.rk.strut.d_wildcard. Useraction "method=" {1} "> <result name=" Success ">/wildcard/user/{1}.jsp</result> </action& Gt

Mapping Case 3

<action name= "*_*" class= "Com.rk.strut.d_wildcard. {1} "method=" {2} "> <result name=" Success ">/wildcard/{1}/{2}.jsp</result> </action>


Complete 3 case sequencing:user_* --- *_* --

        <action name= "user_*"  class= "Com.rk.strut.d_ Wildcard. Useraction " method=" {1} ">        <result name=" success ">/wildcard/user/{1}.jsp</result>        </action>         <action name= "*_*"  class= "Com.rk.strut.d_wildcard . {1} " method=" {2} ">        <result name=" Success ">/ wildcard/{1}/{2}.jsp</result>        </action>         <action name= "*"  class= "Com.rk.strut.d_wildcard. {1} " method=" Add ">            <result  name= "Add" >/wildcard/{1}.jsp</result>        </ Action>





2. Dynamic invocation

Dynamic method Invocation: Dynamically invoke the method in Action by specifying the method name in the URL

Syntax form: Action method name +!+ called by action name


The Struts 2 dynamic method is available by default, and you can disable this feature by adding the constant element in the Struts.xml file:

<constant name= "Struts.enable.DynamicMethodInvocation" value= "true"/>

Complete configuration in the Struts.xml

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.3//en" "http://struts.apache.org/dtds/ Struts-2.3.dtd "><struts> <constant name=" Struts.enable.DynamicMethodInvocation "value=" true "/>< /struts>


Case 1

<action name= "Flower" class= "com.rk.strut.d_wildcard. Floweraction "> <result name=" Add ">/wildcard/flower/add.jsp</result> <result name=" Delete " >/wildcard/flower/delete.jsp</result> </action>

Access Address:

Http://127.0.0.1:8080/hello/rk/wildcard/flower!add.action
Http://127.0.0.1:8080/hello/rk/wildcard/flower!delete.action










STRUTS2 series: (7) wildcard characters and dynamic method calls

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.