Java Spring MVC 将 ArrayList 传递回控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28609414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring MVC passing ArrayList back to controller
提问by user1449511
I am new to Spring. I display a list with users. Every row has a checkbox for removing the users.
我是春天的新手。我显示一个用户列表。每行都有一个用于删除用户的复选框。
Controller:
控制器:
@Controller
public class AdminController {
@Autowired
private UserDao userDao;
@RequestMapping(value = "/admin", method = RequestMethod.GET)
public ModelAndView adminPage() {
ModelAndView model = new ModelAndView();
model.addObject("users", userDao.findAll());
model.setViewName("admin");
return model;
}
@RequestMapping(value = "admin/remove", method = RequestMethod.POST)
public ModelAndView removeUser(@ModelAttribute(value = "users") ArrayList<User> users) {
ModelAndView model = new ModelAndView();
//UPDATE USERS HERE
model.setViewName("redirect:/admin");
return model;
}
JSP:
JSP:
<form:form action="/admin/remove" method="POST" modelAttribute="users">
<table class="table table-striped">
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email/login</th>
<th>Profession</th>
<th>Select<th>
</tr>
</thead>
<tbody>
<c:forEach var="user" items="${users}">
<tr>
<td>${user.firstName}</td>
<td>${user.lastName}</td>
<td>${user.login}</td>
<td>${user.profession}</td>
<td><input type="checkbox" value="${user.delete}"/></td>
</tr>
</c:forEach>
</tbody>
</table>
<input type="submit" value="Delete user(s)" class="btn-danger" />
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
</form:form>
The list is rendered correctly. If i press the "Delete user(s)" button. The @modelAttribute users is empty. I also tried wrapping the list in a new class, but i get the same results.
该列表已正确呈现。如果我按下“删除用户”按钮。@modelAttribute 用户为空。我还尝试将列表包装在一个新类中,但得到了相同的结果。
Any ideas?
有任何想法吗?
采纳答案by user1449511
Thanks to minion, i found the answer
感谢minion,我找到了答案
Wrapper:
包装:
public class UserListWrapper {
private ArrayList<User> users;
public ArrayList<User> getUsers() {
return users;
}
public void setUsers(ArrayList<User> users) {
this.users = users;
}
Controller:
控制器:
@Controller
public class AdminController {
@Autowired
private UserDao userDao;
@RequestMapping(value = "/admin", method = RequestMethod.GET)
public ModelAndView adminPage() {
ModelAndView model = new ModelAndView();
UserListWrapper wrapper = new UserListWrapper();
wrapper.setUsers(new ArrayList<User>(userDao.findAll()));
model.addObject("userListWrapper",wrapper);
model.setViewName("admin");
return model;
}
@RequestMapping(value = "admin/remove", method = RequestMethod.POST)
public ModelAndView removeUser(@ModelAttribute(value = "userListWrapper") UserListWrapper userListWrapper) {
ModelAndView model = new ModelAndView();
userDao.removeFlaggedUsers(userListWrapper.getUsers());
model.setViewName("redirect:/admin");
return model;
}
}
}
View:
看法:
<form:form action="/admin/remove" method="POST" modelAttribute="userListWrapper">
<table class="table table-striped">
<thead>
<tr>
<th>First name</th>
<th>Last name</th>
<th>Email/login</th>
<th>Profession</th>
<th>Select<th>
</tr>
</thead>
<tbody>
<c:forEach varStatus="us" var="user" items="${userListWrapper.users}" >
<tr>
<td><form:input type="hidden" path="users[${us.index}].firstName"/>${user.firstName}</td>
<td><form:input type="hidden" path="users[${us.index}].lastName"/> ${user.lastName}</td>
<td><form:input type="hidden" path="users[${us.index}].login"/>${user.login}</td>
<td><form:input type="hidden" path="users[${us.index}].profession"/>${user.profession}</td>
<td><form:checkbox path="users[${us.index}].delete" value="${user.delete}"/></td>
<form:input type="hidden" path="users[${us.index}].id"/>
</tr>
</c:forEach>
</tbody>
</table>
<input type="submit" value="Delete user(s)" class="btn-danger" />
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
</form:form>
Thank you!
谢谢!
EDIT: Dont forget to also add the fields you are not displaying.
编辑:不要忘记还添加您没有显示的字段。
For example:
例如:
If you dont add the id, your delete will not work because the id in the returned User object will be NULL.
如果不添加 id,则删除将不起作用,因为返回的 User 对象中的 id 将为 NULL。
回答by user1449511
This is because you are using a redirect: in your view. Have a look on Flash Attributes :
这是因为您正在使用重定向:在您的视图中。看看 Flash 属性:
- http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/mvc/support/RedirectAttributes.html
- http://viralpatel.net/blogs/spring-mvc-flash-attribute-example/
- http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/mvc/support/RedirectAttributes.html
- http://viralpatel.net/blogs/spring-mvc-flash-attribute-example/
You should be able to get the updated list :)
您应该能够获得更新的列表:)
回答by minion
Your ModelAttribute is empty as there is no form data binding happening from your jsp to your model attribute. Take a look at how Spring sample for binding collections "http://developer.ucsd.edu/develop/user-interface/building-a-form/form-binding-with-collections.html". This will help you to understand.
您的 ModelAttribute 是空的,因为从您的 jsp 到您的模型属性没有发生表单数据绑定。看看 Spring 示例如何绑定集合“ http://developer.ucsd.edu/develop/user-interface/building-a-form/form-binding-with-collections.html”。这将有助于您理解。
Most of the Spring application typically uses form:inputwith "path" parameter to do data binding.
大多数 Spring 应用程序通常使用带有“ path”参数的form:input来进行数据绑定。
回答by Master Slave
You should build your functionality around spring-mvc selecttag. Few changes would be in order though, push a list to a POJO class e.g.
您应该围绕 spring-mvc select标签构建您的功能。但是,很少有更改,将列表推送到 POJO 类,例如
public class FormBean {
private List<String> users;
public FormBean() {
}
public List<String> getUsers() {
return users;
}
public void setUsers(List<String> users) {
this.users = users;
}
}
change your mapping to
将您的映射更改为
@RequestMapping(value = "admin/remove", method = RequestMethod.POST)
public ModelAndView removeUser(@ModelAttribute(value = "formBean") FormBean formBean) {
finally, swap your c:forEachwith springs selecttag, so something like
最后,用 springs select标签交换你的c:forEach,所以像
<form:form action="/admin/remove" method="POST" modelAttribute="formBean">
...
<form:select path="users" items="${users}" multiple="true" />
...
</form>