您好,我是Spring MVC的新手。我创建了ManytoMany关系(User,Role,User_Roles)。我想插入(User_Roles)。所以我选择了所有角色并将其显示到JSP页面并让用户选择(Multiple = true)。选择我提交请求后。它给出了绑定错误。我认为问题出在UserSetting.jsp中。有人可以帮忙吗?
## 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>
答案 0 :(得分:0)
请发布完整的堆栈跟踪,同时您可以尝试将此添加到控制器:
@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;
}
});
}