Struts 2 reading notes -- use the dynamic method of Action to call

Source: Internet
Author: User

Struts 2 provides actions that process multiple logics, allowing an Action to contain multiple control processing logics. For example, if multiple buttons exist in a page, you can use different actions to process user requests when submitting the same form using different buttons. Such as page

There are two submission buttons on the page, but they are submitted to different actions for processing. The "login" button uses the login logic to process the request, while the "register" button uses the registration logic to process the request.

In this case, you can use a dynamic call method to process such requests. Dynamic method call means that the action of a form element is not directly equal to the name of an Action, but the action attribute of the form is specified in the following form.

The action attribute is actionName! In the methodName format, ActionName specifies the Action to be submitted to, while methodName specifies a method to be submitted to the Action. That is, action = "ActionName! MethodName"

The above Registration button is a button without any action, but clicking this button triggers the regist Function

1 <input type = "submit" value = "register" onclick = "regist ();"> regist function: 2 3 <script type = "text/javascript"> 4 function regist () {5 targetForm = document. forms [0]; 6 targetForm. action = "login! Regist "; 7} 8 </script>

The above regist method changes the form's action attribute and changes the action attribute to "login! The essence of regist is to submit the form to the regist method of LoginAction for processing.

Login | Action class code:

1 public class LoginAction {2 private String username; 3 private String password; 4 public String getUsername () {5 return username; 6} 7 public void setUsername (String username) {8 this. username = username; 9} 10 public String getPassword () {11 return password; 12} 13 public void setPassword (String password) {14 this. password = password; 15} 16 17 // define the login Method 18 public String execute () {19 ActionContext ctx = ActionContext. getContext (); 20 Integer counter = (Integer) ctx. getApplication (). get ("counter"); 21 if (counter = null) {22 counter = 1; 23} 24 else {25 counter = counter + 1; 26} 27 28 ctx. getApplication (). put ("counter", counter); 29 // set the attribute of the session range through ActionContext () 30 ctx. getSession (). put ("user", getUsername (); 31 32 if (getUsername (). equals ("chentmt") & getPassword (). equals ("chenssy") {33 // set the attribute of the reque range through ActionContext () 34 ctx. put ("tip", "server prompt: You have successfully logged on ..... "); 35 return" success "; 36} 37 else {38 ctx. put ("tip", "server prompt: Logon Failed"); 39 return "error "; 40} 41} 42 43 // The registration control logic in Action 44 public String regist () {45 ActionContext. getContext (). getSession (). put ("user", getUsername (); 46 setTip ("congratulations," + getUsername () + ", you have successfully registered .... "); 47 return" success "; 48} 49 50}

 

The above Action Code defines the register control logic included in the change Action. By default, user requests do not submit this method. When you click the "Log on" button, the system submits the default method to LoginAction for processing. When you click "register", the form action is changed to login! The system submits the regist Method for LoginAction modification.

In this way, we can submit different methods to Action by adding multiple processing logic to an action and specifying different Action attributes for the form element.

For a method that uses a dynamic method call, the method declaration of this method is different from that of the default execute method of the system. For other parts, such as the parameter list, the return value type should be the same.

Before calling a qualified dynamic method, you must set Struts 2 to allow dynamic method calls. You can set struts. enable. DynamicMethodInvocation to enable the system's dynamic method call. If this constant is set to true, dynamic method calling is enabled; otherwise, dynamic method calling is disabled.
 

Read Li Gang's lightweight java EE Enterprise Application practices

 

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.