我正在努力将数据绑定到用户和组。我正在使用mongodb。
用户模型
@Document(collection = "users")
public class User {
@Id
private String id;
private String firstName;
private List<Group> groups = new ArrayList<>();
//getters and setters
}
和分组模型
@Document(collection = "groups")
public class Group {
@Id
private String id;
private String name;
//getters and setters
}
用户添加方法
@RequestMapping(value = "/user/add", method = RequestMethod.GET)
public String add(User user, Model model) {
model.addAttribute("groupList",mongoOperations.findAll(Group.class));
return "users/add";
}
还有百里香html
<form th:object="${user}" method="post">
<select multiple class="form-control" th:field="${user.groups}" >
<option th:each="grp : ${groupList}"
th:text="${grp.name}"
th:value="${grp.id}" />
</select>
</form>