Use wildcards in "struts2" action

Source: Internet
Author: User

In the previous study, the configuration of the,<action> element is explicitly configured, and its name, class, and other attributes are a definite value. In fact Struts2 also supports the class attribute and the method property using wildcards from the Name property.

1) Next look at the example using wildcards if we use the following <action> configuration:

<action name= "*_*" class= "cn.javass.action.action. {1} Action "method=" {2} ">       <result name=" Towelcome ">/s2impl/welcome.jsp</result>  

In the above configuration:

The value "*" in the Name property represents any string that is not 0 in length, so it can respond with an action that requires only an underscore in the middle of the name. For example, the page can be accessed by the action name: helloworld_create.action, HelloWorld _update.action, and so on.

After the Name property has a wildcard character defined, the Class property uses the first wildcard character (using {1} as a placeholder) and the method property uses the second wildcard character.

If you use Helloworld_create.action as the action name of the access, the action name in Struts.xml is called Helloworld_create, the first wildcard matches HelloWorld, A second wildcard matches the create. Therefore, the Cn.javass.action.action.HelloWorldAction's Create method responds.

2) So, for the <result> element, can you also use the wildcard character defined by the <action> element's name attribute? The answer is yes, if you have the following configuration:

<action name= "*_*_*_*" class= "cn.javass.action.action. {1} Action "method=" {2} ">       <result name=" {3} ">/${folder}/{4}.jsp</result>  </action>  

3) When using wildcards, it is also possible that more than one <action> element using wildcards might match the access to this URL, looking at the following configuration file:

<action name= "Helloworld_create" class= "cn.javass.action.action.HelloWorldAction" method= "Create2" >        <result name= "Towelcome" >/s2impl/welcome.jsp</result>  </action>  <action name= "*_*" Class= "Cn.javass.action.action. {1} Action "method=" {2} ">        <result name=" Towelcome ">/s2impl/welcome.jsp</result>  </action >  

At this time, if you visit "/helloworld/helloworld_create.action", Struts2 will first find out if there is an exact matching <action> element at this time, regardless of the above <action> In what order the elements appear, STRUTS2 will definitely find and use the exact <action> element. However, if the <action> element is not exactly matched, Struts2 will find the first matching <action> element to use the wildcard character.

Wildcards for simple CRUD engineering or software prototypes, as long as the package name of the action, the class name of the action, the corresponding method name written by the regular application, can greatly simplify the work of the configuration.

Reference: http://www.iteye.com/topic/1124526

Use wildcards in "struts2" action

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.