Struts 2 automatically receives parameters by action

Source: Internet
Author: User

There are three main methods of action receiving parameters in Struts2:

1. Receive parameters using the properties of the action: (via Property-driven)

A. Definition: Define attributes in the action class, create get and set methods;

B. Receive: Receive parameters through attributes, such as: UserName;

C. Send: Use property name to pass parameters, such as: USER1!ADD?USERNAME=MAGCI;

Jsp:

<form action= "Sys/login.action" method= "POST" >  
 <input type= "text" name= "username" >  
 <input Type= "Submit" value= "Submit" >  
</form>

Action: Gets directly through the get, set method.

public class Sysaction extends actionsupport{  
 private String username;  
        
 Public String Login () throws Exception {  
  System.out.println (username);  
  return SUCCESS;  
 }  
        
 Public String GetUserName () {return  
  username;  
 }  
 public void Setusername (String username) {  
  this.username= username;  
 }  
}

2. Use Modeldriven to receive parameters: (model-driven, the Modeldriven<t> interface must be implemented.) Inconvenient for the second way to pass in multiple model

More Wonderful content: http://www.bianceng.cn/Programming/Java/

A. Definition: The action implements the Modeldriven generic interface, defines the object of the model class (must be New), and returns the object through the Getmodel method;

B. Receive: Parameters are received through the properties of the object, such as: User.getusername ();

C. Send: Pass parameters directly using attribute names, such as: USER2!ADD?USERNAME=MGC

Jsp:

<form action= "Sys/login.action" method= "POST" >  
 <input type= "text" name= "username" >  
 <input Type= "Submit" value= "Submit" >  
</form>

Action: The Getmodel () method must be implemented

public class Sysaction extends Actionsupport implements modeldriven<user>{  
 Private user user;  
       
 Public String Login () throws Exception {  
  System.out.println (Getmodel (). GetUserName ());  
  return SUCCESS;  
 }  
       
 Public User Getmodel () {  
  if (null = = user) {return  
   user = new User ();  
  }  
  return user;  
 }  

3. Use Domainmodel to receive parameters: (the third domain model method can not implement MODELDRIVEN<T> at all, or you can use the properties of multiple model objects. )

A. Definition: Define the Model class, define the object of the model class (without new) in the action, and create the Get and set methods of the object;

B. Receive: Parameters are received through the properties of the object, such as: User.getusername ();

C. Send: Use the properties of the object to pass parameters, such as: USER2!ADD?USER.USERNAME=MGC;

Jsp:

<form action= "Sys/login.action" method= "POST" >  
 <input type= "text" name= "User.username" >  
 < Input type= "text" name= "Teacher.level" >  
 <input type= "Submit" value= "Submit" >  
</form>

Action: The set method must be supplied

public class Sysaction extends actionsupport{  
 Private user user;  
 Private Teacher Teacher;  
       
 Public String Login () throws Exception {  
  System.out.println (User.getusername ());  
  System.out.println (Teacher.getlevel ());  
  return SUCCESS;  
 }  
       
 public void SetUser (user user) {  
  this.user = user  
 }  
 public void Setteacher (Teacher Teacher) {  
  this.teacher = Teacher;  
 }  
}

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.