There are three main methods of action receiving parameters in Struts2:
1. Receive parameters using the properties of the action:
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;
2. Use Domainmodel to receive parameters:
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;
3. Use Modeldriven to receive parameters:
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
Struts2 Action gets table conveys value
1. Through property-driven
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. Model-driven mode, the Modeldriven<t> interface must be implemented. Inconvenient for the second way to pass in multiple model
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. The third Way is to not implement MODELDRIVEN<T> at all, or to use the properties of multiple model objects.
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 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; }
}