Struts dynamic method call changes the form action attribute, strutsaction
1. Dynamic Method call (DMI: Dynamic Method Invocation)
Javasstruts2 also provides this Action that contains multiple logic businesses, so that you can process multiple business logic in one Action. For example, when a user uses different submit buttons to submit the same form, the system uses different methods to process different user requests, in this case, the same Action must contain multiple control processing logics.
Callback dynamic method calls include:
①. Change the method attribute in the action in struts. xml.
② Change the action attribute in the form to change the request logic of different submissions.
③ Use wildcards.
Ii. Simple Example (modifying the action attribute in form ):
① First, a form is displayed. There are two submit buttons in the form, but they represent different services. When you click "Log on", the user logs on. When you click "register", the user registers.
② User Logon:
③ User registration:
Detailed code (this example is based on a simple example of struts2, so the establishment of struts2 here is not detailed demonstration, if you have doubts about struts2 request to see: http://www.cnblogs.com/demoMeng/p/5841976.html ):
1. log on to the registration page (index. jsp): In DMI, the following script section is the key to change the action attribute in the form. Other struts. you only need to configure the xml file.
<% @ Page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd"> <Script src = "js/jquery-1.7.2.js"> </script> <title> index </title>
<Script type = "text/javascript"> $ (function () {$ ("input: eq (3 )"). click (function () {// obtain the form and change the attribute value of the action $ ("# form "). attr ("action", "userCreate ");});}); </script>
② Struts. xml: Configuration File
<struts> <package name="myP" extends="struts-default"> <action name="in" class="action.Action" method="go"> <result name="login">WEB-INF/jsp/index.jsp</result> </action> <action name="userLogin" class="action.Action" method="test"> <result name="userLogin">WEB-INF/jsp/userLogin.jsp</result> </action> <action name="userCreate" class="action.Action" method="create"> <result name="userCreate">WEB-INF/jsp/userCreate.jsp</result> </action> </package> </struts>
③ Action class:
Package action; import com. opensymphony. xwork2.ActionSupport; public class Action extends ActionSupport {private String name; private String password; public String getName () {return name;} public void setName (String name) {this. name = name;} public String getPassword () {return password;} public void setPassword (String password) {this. password = password;} public String go () {System. out. prin Tln ("go to the registration page !! "); Return" login ";} public String test () {System. out. println ("User Logon"); return "userLogin";} public String create () {System. out. println ("User Registration"); return "userCreate ";}}
This example is just a simple DMI method, and does not include too many business logic processing such as: whether the user login is correct and meets the conditions. This is just a simple example. The actual business needs to be further modified and analyzed. Thank you for browsing.