struts action接收list对象

 

说明:struts2的action接收List<Object>。

有javabean:User,如下:

public class User {
// Fields

private Integer id;
private String username;
private String password;
public Integer getId() {
return id;
}

// Default Constructor
public User() {

}

// mini Constructor
public User(Integer id) {
this.id = id;
}

// Property accessors

public void setId(Integer id) {
this.id = id;
}
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;
}

@Override
public String toString() {
return username+","+password;
}
}

Action类:UserAction,如下:

public class UserAction extends ActionSupport {
private User user;
private List<User> userList;

public List<User> getUserList() {
return userList;
}

public void setUserList(List<User> userList) {
this.userList = userList;
}

public User getUser() {
return user;
}

public void setUser(User user) {
this.user = user;
}

/**
* 批量修改User。
* @return
*/
public String addUser() {
for(User user2:userList) {
System.out.println(user2.toString());
}
return SUCCESS;
}
}

JSP页面:user.jsp,如下:

<%@ page language="java" contentType="text/html;"
pageEncoding="utf-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>修改用户</title>
</head>
<body>
<s:form action="updateUser.action" method="post">
<table>
<tr>
<td>
用户名
</td>
<td>
密码
</td>
</tr>
<s:iterator value="userList" status="stat">
<tr>
<td>
<s:textfield name="userList[%{#stat.index}].username"></s:textfield>
</td>
<td>
<s:password name="userList[%{#stat.index}].password"></s:password>
</td>
</tr>
</s:iterator>
</table>
</s:form>
</body>
</html>

你可能感兴趣的:(struts)