Controller:
@GetMapping("/access") public ModelAndView accessView() { ... modelAndView.addObject("newUser", new User()); User.class:
@ManyToMany(...) private Set<Role> roles = new HashSet<>(); public User() { this.id = 1L; this.username = "1212121"; Role role1 = new Role(); role1.setId(1L); role1.setNameRole("QWERTY"); Role role2 = new Role(); role2.setId(2L); role2.setNameRole("ASDFGH"); Role role3 = new Role(); role3.setId(3L); role3.setNameRole("ZXCVBN"); this.roles.add(role1); this.roles.add(role2); this.roles.add(role3); }; Jsp page:
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%> ... <form:form method='POST' ... modelAttribute="newUser"> ... <form:select path="roles" multiple="true" id="userRoles_" class="form-control"> <form:options items="${roles}" itemLabel="nameRole" itemValue="id"/> </form:select> I pass the User object, how to fill out form from the user’s collection on the page from the user role collection: select?