Struts2接受参数的几种类型 

大概有这几种类型:

1.使用Action的属性接受参数

       在Action中加入成员变量,配置Getter和Setter方法,Getter而和Setter方法的名字和表单中input标签的name属性一致(简单来说就是Action中变量的名和表单中input的name值一致)

2.使用DomainModel接收参数

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类:

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类:

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.使用ModelDriven接收参数(推荐使用)

   和Domain主要不同:

    1.User对象需要实例化

    2.不需要给User对象配置getter和setter方法

    3.需要实现ModelDriven接口

    4.JSP中input的name属性不需要加对象.(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>

USER类和上一样。。

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


(当然获取request后再取参数也是可以的)
    

接受复杂类型参数(list<String>和list<Object>)

1、参数是简单的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类:

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类:

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.接受复杂类型参数(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类:

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类:

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

posted on 2016-06-13 22:30  Flink菜鸟  阅读(5539)  评论(0编辑  收藏  举报