"Turn from" http://www.cnblogs.com/bukudekong/archive/2012/03/29/2423064.html
There are three main ways to receive parameters in the Struts2 action:
1. Use the action's properties to receive parameters:
A. Definition: Define properties in the action class, create get and set methods;
B. Receive: Receive parameters via attributes, such as: UserName;
C. Send: Use attribute names 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 in action (no new is required), and create a Get and set method for the object;
B. Receive: Receive parameters through the object's properties, such as: User.getusername ();
C. Send: Use the object's attributes 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), returns the object by means of the Getmodel method;
B. Receive: Receive parameters through the object's properties, such as: User.getusername ();
C. Send: Pass parameters directly using attribute names, such as: USER2!ADD?USERNAME=MGC
There are three main ways to receive parameters in the Struts2 action:
There are three main ways to receive parameters in the Struts2 action:
1. Use the action's properties to receive parameters:
A. Definition: Define properties in the action class, create get and set methods;
B. Receive: Receive parameters via attributes, such as: UserName;
C. Send: Use attribute names 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 in action (no new is required), and create a Get and set method for the object;
B. Receive: Receive parameters through the object's properties, such as: User.getusername ();
C. Send: Use the object's attributes 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), returns the object by means of the Getmodel method;
B. Receive: Receive parameters through the object's properties, such as: User.getusername ();
C. Send: Pass parameters directly using attribute names, such as: USER2!ADD?USERNAME=MGC
Struts2 action Get table only son value
1. By attribute-driven
Jsp:
<form action="sys/login.action"method="post"> <input type="text" name="username"> <input type="submit"value="submit"></form> |
Action: Obtained directly from the GET, set method.
publicclasssysAction extendsActionSupport{ privateString username; publicString login() throwsException { System.out.println(username); returnSUCCESS; } publicString getUsername() { returnusername; } publicvoid setUsername(String username) { this.username= username; }} |
2. Model-driven mode, you must implement Modeldriven<t> interface. Inconvenient for the second way to pass 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 can be completely non-modeldriven<t> You can also 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 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;
}
}
Method of action receive parameter in STRUTS2