如何通过spring mvc接收页面表单List
1个回答
展开全部
1.在表单提交之前,把表单序列化成JSON格式传到后台,在来解析封装成List<User>.
2.我们创建一个UserModel
public class UserModel {
private List<User> users;
public List<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
public UserModel(List<User> users) {
super();
this.users = users;
}
public UserModel() {
super();
}
}
修改我们的控制层和页面
1 @RequestMapping("/user")
2 public void test(UserModel userModel ){
3 logger.debug(JSONArray.toJSON(userModel));
4 }
<%@ page language="java" import="java.util.*" pageEncoding="UTF
-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>表单批量提交</title>
</head>
<body>
<form action="user" method="post">
用户名:<input type="text" name="users
[0].username"><br/>
密码:<input type="text" name="users
[0].password"><br/>
用户名:<input type="text" name="users
[1].username"><br/>
密码:<input type="text" name="users
[1].password"><br/>
<input type="submit">
</form>
</body>
</html>
2.我们创建一个UserModel
public class UserModel {
private List<User> users;
public List<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
public UserModel(List<User> users) {
super();
this.users = users;
}
public UserModel() {
super();
}
}
修改我们的控制层和页面
1 @RequestMapping("/user")
2 public void test(UserModel userModel ){
3 logger.debug(JSONArray.toJSON(userModel));
4 }
<%@ page language="java" import="java.util.*" pageEncoding="UTF
-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>表单批量提交</title>
</head>
<body>
<form action="user" method="post">
用户名:<input type="text" name="users
[0].username"><br/>
密码:<input type="text" name="users
[0].password"><br/>
用户名:<input type="text" name="users
[1].username"><br/>
密码:<input type="text" name="users
[1].password"><br/>
<input type="submit">
</form>
</body>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询