struts2 dynamic method call exclamation!
Struts2 's Dynamic method invocation (INVOC) is summarized in three ways, as described below:
1, the first way: Set the Method property
Define a method in the action class with the same signature as the Execute method, but with a different name, as defined:
Public String Login () throws exception{}
Then add a <action> element to the Struts.xml file and set its Method property. The code is as follows (core code):
<action name= "Loginmethod"
class= "com.polaris.LoginAction" method= "Login" >
<result>/result.jsp</result>
<result name= "Error" >/error.jsp</result>
</action>
List 1:struts.xml part of the content
The form's action is then set to Loginmethod in the JSP. In this way, STRUTS2 will call the Loigin method in Loginaction without invoking the Execute () method.
2, the second way: Change the form's action to set the content
In the first way, set a login method. Just don't need to change the contents of the Struts.xml file. Then, in the JSP page, set the form's action to loginmethod!login.action. In this way, STRUTS2 also calls the login method without invoking the Execute method. Where form action= "Loginmethod!login.action", Loginmethod is the name attribute value of the <action> element in the Struts.xml file, Login in Login.action is the name of the method in the action class. Note: login.action action is not limited.
Summary: The format of the URL is: (The Name property value of the action tag). (corresponds to the method name in the implementation Aciton)
3. Third Way: Use wildcard characters
Start with the above two methods, a login method, and then configure the following in the Struts.xml file:
<action name= "*action"
class= "Com.xuxinhua1984.struts2.i18n.LoginAction" method= "{1}" >
<result name= "Success" >/success.jsp</result>
<result name= "Error" >/error.jsp</result>
</action>
List 2:struts.xml part of the content
Then in the JSP page, set the action for the form loginaction, where login is the method name in the action class, so that *action in Struts.xml can match loginaction Blur, and then method= "{1 } "is equivalent to method=" login ". This is the first way back.
In addition, this way even the physical view corresponding to the result type can be used as a wildcard character. For example, if you want to return to the login page after a failed login, you can set <result name= "error" >/{1}.jsp</result>. Thus, the {1} here will be replaced by the actual content of the * number, which is login, so it is forwarded back to the login.jsp page.
Summary: For the above three ways, I personally recommend the use of the first, the second way, feel that the third way is more flexible, but also prone to confusion, and error prone.
STRUTS2 supports dynamic method invocation, which means that there are multiple methods in an action, and the system accesses different methods based on the action given by the form element instead of writing multiple actions.
The way to call the action non-execute method directly in Struts2 without configuration is to try the struts2 dynamic dynamic method call.
Dynamic method Invocation (INVOC) is 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 configuration is as follows:
<action name= "Login" class= "Com.pj.action.LoginAction" >
<result type= "JSON" ></result>
</action>
When/login!query.action is requested, the Loginaction query () method is called, and Loginaction's Save () method is called when the/login!save.action is requested.
notes
STRUST2 provides a configuration to disable DMI, which you can use to turn off DMI by using the constant element to set the Struts.enable.DynamicMethodInvocation property to False in the Struts.xml file.