spring boot thymeleaf多重选择数据绑定mongodb

时间:2018-06-30 08:22:29

标签: java spring mongodb spring-boot

我正在努力将数据绑定到用户和组。我正在使用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>

0 个答案:

没有答案