Hi i am new in Spring MVC. I have created ManytoMany relation (User, Role, User_Roles). I am trying to insert into (User_Roles). So i selected all Roles and showed it to JSP page and Give user to select (Multiple=true). After Selecting when i submit the request. It gives binding error. I think the problem is in UserSetting.jsp. Can anyone help?
## BINDING ERROR ##
HTTP ERROR 400
Problem accessing /DistributionSystem/submitUser. Reason:
Bad Request
UserEntity.java
private Set<RoleEntity> roles = new HashSet<>(0);
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "USER_ROLES", joinColumns = {
@JoinColumn(name = "ROLE_ID", nullable = false, updatable = false) },
inverseJoinColumns = { @JoinColumn(name = "USER_ID",
nullable = false, updatable = false) })
public Set<RoleEntity> getRoles() {
return roles;
}
}
public void setRoles(Set<RoleEntity> roles) {
this.roles= roles;
}
Controller.java
@RequestMapping(value = "/submitUser", method = RequestMethod.POST, params = "add")
protected String addUser(Model model, @ModelAttribute("user") UserEntity user){//,@RequestParam("userRoles") List<Integer> userRoles) {
//System.out.println(userRoles.size());
System.out.println("======="+user.getRoles().size());
//userService.addUser(user);
return "userSettingsPage";
}
UserSetting.jsp
<div class="alert-alert-success">
<div class="col-sm-3">
<label>Roles</label>
<div class="form-inline">
<div class="col-md-4">
<select id="dates-field2" class="multiselect form-control" multiple="multiple" name="roles">
<c:forEach items="${userRoles}" var="role">
<option value=${role}> ${role.name} </option>
</c:forEach>
</select>
</div>
</div>
</div>
</div>
</div>
Please post complete stacktrace, meanwhile you can try this adding to your Controller :
@InitBinder
public void webFormDataBinder( WebDataBinder webDataBinder ){
webDataBinder.registerCustomEditor(Set.class, "roles", new CustomCollectionEditor(Set.class){
RoleEntity roleEntity = null;
@Override
protected Object convertElement(Object element) {
roleEntity = new RoleEntity();
roleEntity.setRoleId( element.toString() );
return roleEntity;
}
});
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.