Dynamic Action Implementation of Struts2

From: Dynamic Action implementation of Struts2

There are three ways of dynamic method invocation in Struts2.

 

1. Specify the method attribute Specify
the method attribute of the action in struts.xml.

<package name=”demo1” extends=”struts-default”>
              <action name=”Login” class=”com.demo.LoginAction” />
                     <result name=”input”>/input.jsp</result>
                     <result name=”error”>/error.jsp</result>
                     <result name=”success”>/success.jsp</result>
              </action>
              <action name=”Registry” class=”com.demo.LoginAction” method=”registry” />
                     <result name=”input”>/input.jsp</result>
                     <result name=”error”>/error.jsp</result>
                     <result name=”success”>/success.jsp</result>
              </action>
       </package>

The processing logic is specified by the method method. The processing logic corresponding to the Action named Login is the default execute method, and the processing logic corresponding to the Action named Registry is the registry method specified by the method.

 

Second, the exclamation mark method (need to be turned on), the official website does not recommend using this method, it is recommended that you do not use it.

 

3. Wildcard method (recommended on the official website)

When configuring the <action> element, you need to specify attributes such as name, class, and method. These three attributes support wildcards. When using wildcards to define the name attribute of an Action, it is equivalent to defining multiple logical actions for one element action.

The action configuration in the first method can be changed to:

<package name=”demo” extends=”struts-default”>
       <action name=”*Actionclass=”com.demo.LoginAction” method=”{1}”>
              <result name=”input”>/input.jsp</result>
              <result name=”error”>/error.jsp</result>
              <result name=”success”>/success.jsp</result>
       </action>
</package>

 The above definition does not define an ordinary action, but defines a series of actions. As long as the URL requested by the user satisfies the *Action pattern, it can be processed by the action, and the method attribute uses an expression {1}, the The value of the expression is the value represented by the first * in the name attribute. For example, if the URL requested by the user is LoginAction, the * represents the Login string, so the Login method of the com.demo.LoginAction class is called to process it. . If the requested URL is RegistryAction, call the Registry method of com.demo.LoginAction for processing.

 

The following configuration uses wildcards in the class attribute:

<package name=”demo” extends=”struts-default”>
       <action name=”*Action” class=”com.demo.{1}Action”>
              <result name=”input”>/input.jsp</result>
              <result name=”error”>/error.jsp</result>
              <result name=”success”>/success.jsp</result>
       </action>
</package>

The method attribute is not specified in this configuration, so the request is executed by the default execute method, but wildcards are used in the class, and its meaning is the same as above. For example, when the user requests LoginAction, the value of * is Login, the The value is passed into the class attribute, that is, the processing class of the Action is com.demo.LoginAction; and if the request is RegistryAction, the processing class of the Action will become com.demo.RegistryAction.

Struts2 allows expressions to be used in both the class attribute and the method attribute. Examples are as follows:

<action name=”*_*” class=”com.demo.{1} method=”{2}” />

Actions that satisfy the *_* pattern will be processed by it. For example, an Order_Booking request arrives. Since the value of the first * is Order and the value of the second * is Booking, it means that com.demo.Order will be called for processing. The Booking method in the class to handle the user request.

 

Struts2 can also use expressions in the <result> element, as follows:

<action name=”*Action” class=”com.demo.{1}Action method=”{1}” >
              <result name=”success”>/{1}.jsp</result>
       </action>

When the request is LoginAction, the Login method in the com.demo.LoginAction processing class will be called to process the user's request, and when the return is success, the /Login.jsp page will be displayed.

 

Note: After using wildcards, unless the requested URL is absolutely the same as the Action's name attribute, it will be determined which Action handles the user request according to the order defined by the Action in struts.xml.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324949481&siteId=291194637