Struts2 accept several types of parameters
There are probably several types of these:
1. Accept parameters using the action's properties
Add member variables to the action, configure getter and setter methods, getter and the name of the setter method is the same as that of the input tag in the form (simply because the name of the variable in action matches the name of input in the form)
2. Using Domainmodel to receive parameters
Jsp:
<form action= "Loginaction.action" method= "POST" >
User: <input type= "text" name= "user.username" ><br> Password: <input
type= "Password" name= "user.password" > <br>
<input type= "Submit"
Value= "Submit" >
</form>
User class:
public class User {
Private String username;
private String password;
Public String GetUserName () {
return username;
}
public void Setusername (String username) {
This.username = Username;
}
Public String GetPassword () {
return password;
}
public void SetPassword (String password) {
This.password = password;
}
}
Action class:
public class Loginaction extends Actionsupport implements modeldriven<user> {
private user user;
Public String Login () {
System.out.println ("username =" + user.getusername ());
SYSTEM.OUT.PRINTLN ("password =" + User.getpassword ());
return SUCCESS;
}
Public User GetUser () {
return user;
}
public void SetUser (user user) {
This.user = user;
}
}
3. Use Modeldriven to receive parameters (recommended)
differs from domain primarily:
1.User objects need to be instantiated
2. Do not need to configure getter and setter methods for user objects
3. Need to implement Modeldriven interface
The Name property of input in 4.JSP does not need to add an object. (user.)
Jsp:
<form action= "Loginaction.action" method= "POST" >
User: <input type= "text" name= "username" ><br> Password: <input
type= "Password" name= "password" > <br>
<input type= "Submit"
Value= "Submit" >
</form>
The user class is the same as the above:
Action:
public class Loginaction extends Actionsupport implements modeldriven<user> {
Private User user = new user ();
Public String Login () {
System.out.println ("username =" + user.getusername ());
SYSTEM.OUT.PRINTLN ("password =" + User.getpassword ());
return SUCCESS;
}
@Override
Public User Getmodel () {
return user;
}
}
(It is also possible to retrieve the request and then take the parameters)Accept Complex Type parameters (List<string> and list<object>)
1, the parameter is simple list<string>
Jsp:
<form action= "Loginaction.action" method= "POST" >
User: <input type= "text" name= "username" ><br> Password: <input
type= "password" name= "password" > <br>
Book1: <input type= "text" Name= "booklist[0]" ><br>
BOOK2: <input type= "text" Name= "booklist[1]" ><br>
BOOK3: <input type= "text" Name= "booklist[2]" ><br>
<input type= "Submit"
Value= "Submit" >
</form>
User class:
public class User {
Private String username;
private String password;
Private list<string > Booklist;
Public list<string > Getbooklist () {
return Booklist;
}
public void Setbooklist (List<string > Booklist) {
This.booklist = Booklist;
}
Public String GetUserName () {
return username;
}
public void Setusername (String username) {
This.username = Username;
}
Public String GetPassword () {
return password;
}
public void SetPassword (String password) {
This.password = password;
}
}
Action class:
public class Loginaction extends Actionsupport implements modeldriven<user> {
Private User user = new user ();
Public String Login () {
System.out.println ("username =" + user.getusername ());
SYSTEM.OUT.PRINTLN ("password =" + User.getpassword ());
For (String book:user.getBookList ()) {
System.out.println ("book=" + book);
}
return SUCCESS;
}
@Override
Public User Getmodel () {
return user;
}
}
2. Accept Complex Type parameters (list<object>)
Jsp:
<form action= "Loginaction.action" method= "POST" >
User: <input type= "text" name= "username" ><br> Password: <input
type= "password" name= "password" > <br>
Book1: <input type= "text" name= "Userlist[0].username" ><br>
BOOK2: <input type= "text" name= "Userlist[1].username" ><br>
BOOK3: <input type= "text" name= "Userlist[2].username" ><br>
<input type= "Submit"
Value= "Submit" >
</form>
User class:
public class User {
Private String username;
private String password;
Private list<user> userlist;
Public list<user> getuserlist () {
return userlist;
}
public void Setuserlist (list<user> userlist) {
This.userlist = userlist;
}
Public String GetUserName () {
return username;
}
public void Setusername (String username) {
This.username = Username;
}
Public String GetPassword () {
return password;
}
public void SetPassword (String password) {
This.password = password;
}
}
Action class:
public class Loginaction extends Actionsupport implements modeldriven<user> {
Private User user = new user ();
Public String Login () {
System.out.println ("username =" + user.getusername ());
SYSTEM.OUT.PRINTLN ("password =" + User.getpassword ());
For (User u:user.getuserlist ()) {
System.out.println ("User =" + U.getusername ());
}
return SUCCESS;
}
@Override
Public User Getmodel () {
return user;
}
}
STRUTS2 accepts several types of parameters and accepts complex type parameters (List<string> and list<object>)