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;
}
}