Struts2 dynamic call methods and using wildcard definitions

Source: Internet
Author: User

The way that the action non-execute method can be called directly without configuration in Struts2 is called with the struts2 dynamic method. The dynamic method call is to use an exclamation mark (!) in the name of the action. To indicate the name of the method to invoke, with the syntax formatted as actionname!methodname.action, for example our Struts.xml is configured as follows:

    <struts>                <package name= "struts" namespace= "/user" extends= "Struts-default" >              <action name= " User "class=" com.struts.action.UserAction ">                  <result name=" Success ">/web-inf/page/message.jsp</ result>              </action>          </package>                  </struts>    

The following methods are available in the action:

    Public String AddUser () {          this.message = "AddUser";          Return "Success";      }            Public String DeleteUser () {          this.message = "DeleteUser";          Return "Success";      }  

When/user!adduser.action is requested, the Useraction AddUser () method is called, and Useraction's DeleteUser () method is called when/user!deleteuser.action is requested.
If you want to disable dynamic method calls, you can configure the following constants in Struts.xml

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

To define an action using a wildcard character

Use wildcard characters: The advantage is that you can greatly reduce the contents of the configuration file, as an example:

The configuration in Struts.xml is as follows:

    <struts>                <package name= "struts" namespace= "/user" extends= "Struts-default" >              <action name= " User_* "class=" com.struts.action.UserAction "method=" {1} ">                  <result name=" Success ">/web-inf/page/ message.jsp</result>              </action>          </package>                  </struts>  

User_* * is a wildcard character, that is, can match any character, method= "{1}" in the {1} is the content of the *, of course, can also use multiple *, such as "*_*", "*_*_*", but it is recommended not to use too much *, which will reduce the readability of the Code

Methods in the action:

    Public String AddUser () {          this.message = "AddUser";          Return "Success";      }            Public String DeleteUser () {          this.message = "DeleteUser";          Return "Success";      }  

When/user_adduser.action is requested, the Useraction AddUser () method is called, and Useraction's DeleteUser () method is called when/user_deleteuser.action is requested.

About the precedence of wildcard matching:

(1) If there is a corresponding action name in the Struts.xml, even if it has other wildcard matches, it will take precedence over the exact same.
For example, there is an action name of "User_add" and one is "user_*". Now that a request is "user_add.action", it will first match "User_add".

(2) If an action name corresponds to an action name of two wildcard characters, then you need to look at this configuration who is in front of it, and it matches the one written in the previous
For example, one action name is "*_*" and one is "user_*", and now there is a request for "user_add.action". Then it will first match the action written in front of it.


(3) Any action with "*" name priority is the same, not that the priority with a "*" is higher than with two "*".

Summary: So we should put the Acton configuration with the most wildcard characters at the end, otherwise Struts2 one by one matches all actions, which reduces the efficiency of the program.

Struts2 dynamic call methods and using wildcard definitions

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.