Company
在 Set
中有一些 User
实体,所有用户都存储在DB中 . 我想以HTML格式选择一些使用 multiple-select
的用户 . 使用Thymeleaf和Spring(MVC,Boot) .
我_752502已经尝试过@InitBinder,Spring Core Converter,但没有任何效果 . 问题: @Controller failes on bindingResult.hasErrors():
@Controller
@RequestMapping(value = { "/add" }, method = { RequestMethod.POST })
public String saveNew(@Validated @ModelAttribute("company") Company company, BindingResult bindingResult, Model model) {
if (bindingResult.hasErrors())
Company bean
public class Company {
private Set<User> users = new HashSet<User>();
Thymeleaf HTML form
<form th:object="${company}">
<select th:field="*{users}" multiple="multiple">
<option th:each="user : ${allUsers}" th:value="${user.id}" th:text="${user.email}"></option>
</select>
如何实现这种多选的正确方法是什么?
1 回答
你可以使用这段代码
(在th:value中看起来是双{{}}) .
现在你需要一个这样的格式化程序: