Struts2.5 action dynamic method call (transfer)

Struts 2.5 needs to implement an action to call different methods. Generally speaking, there are three ways:

One, specify the method attribute.

<action name="login" class="com.action.XXXAction" method="redirect">
            <!-- redirectAction return type -->
            <result type="redirectAction">
                <param name="actionName">userLogin</param>  <!-- Action名参数 -->
                <param name="namespace">/user</param> <!-- namespace parameter -->
            </result>
</action>

Second, the exclamation mark method. (It needs to be turned on, although the official website does not recommend using this method, but I still use it!)

First turn on a switch:

<struts>
    <constant name="struts.enable.DynamicMethodInvocation" value="true" />
    <!-- Configuration package default, which inherits struts-default -->
    <package name="default" extends="struts-default">

The above code generally works at the beginning. It should be noted that 2.5 not only needs to turn on this switch, but also needs to declare the methods you access:

<package name="default" namespace="/" extends="struts-default">
        <global-allowed-methods>Here is the method name you want to declare common to all actions in this package</global-allowed-methods>
        <!-- Configure the Action named login, and the implementation class is LoginAction -->
        <action name="login" class="com.action.LoginAction" >
            <!-- redirectAction return type -->
            <result type="redirectAction">
                <param name="actionName">userLogin</param>  <!-- Action名参数 -->
                <param name="namespace">/user</param> <!-- namespace parameter -->
            </result>
            <!-- The return value is error, redirectAction type, redirect to Action named error -->
            <result name="error" type="redirectAction">error</result>
            <allowed-methods>getoper,getmobile</allowed-methods>
            <!-- Here is the unique method name of this action you want to declare, multiple method names are separated by commas -->
        </action>

Third, the wildcard method. (This method is recommended by the official website!)

<action name="login_*" class="com.action.LoginAction" method="{1}">
            <!-- redirectAction return type -->
            <result type="redirectAction">
                <param name="actionName">userLogin</param>  <!-- Action名参数 -->
                <param name="namespace">/user</param> <!-- namespace parameter -->
            </result>
            <!-- The return value is error, redirectAction type, redirect to Action named error -->
            <result name="error" type="redirectAction">error</result>
            <allowed-methods>redirect</allowed-methods>
        </action>
It should be noted in this method that the switch in method 2 must be turned off first, that is,
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
Change true to false. struts2.5.5 is the default false.
And also need to declare the method name allowed to access in <allowed-methods>,
To access the methods of this method just need to be in the jsp file
<s:a action="login_redirect">redirect</s:a>
This way I can access the redirect() method I declared earlier. The * in login_* is a wildcard.

 

Guess you like

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