0
我開始使用Spring,並且發現了一個我無法理清的問題。 我有一個用戶的實體,具有「角色」屬性,「角色」是一個集合對象,該estity如下:Spring MVC - 將屬性設置爲控制器中的@ModelAttribute對象
private String id;
private String name;
private String surname;
private String email;
private String identificationNumber;
private String username;
private String password;
private String passwordConfirm;
private int sex;
private Timestamp birthDate;
private Set<Role> roles;
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "user_role", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
public Set<Role> getRoles() {
return roles;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
我的「角色」實體如下:
private String id;
private String name;
private Set<User> users;
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@ManyToMany(mappedBy = "roles")
public Set<User> getUsers() {
return users;
}
public void setUsers(Set<User> users) {
this.users = users;
}
在JSP中我有一個列表中選擇一個組合框的所有「角色」
<spring:bind path="roles">
<div class="input-field is-empty cell2">
<form:select type="text" path="roles" class="validate ${status.error ? 'invalid' : ''}">
<form:option value="NONE" selected="selected" disabled="true">Roles</form:option>
<c:forEach items="${allRoles}" var="role">
<form:option value="${role.getId()}" >${role.getName() }</form:option>
</c:forEach>
</form:select>
<form:errors path="roles" class="alert alert-dismissible alert-danger"> </form:errors>
<span class="material-input"></span>
</div>
</spring:bind>
我選擇一個或多個「角色」,並提交形式如下控制器,在此存在@RequestParam與ID列表所選'角色'
@RequestMapping(value="/user_edit/{id}", method=RequestMethod.POST)
public String edit_user(@ModelAttribute("userForm") User userForm, BindingResult bindingResult, @PathVariable String id, @RequestParam String role_ids) {
Set<Role> role_list = new HashSet<Role>();
for (String role_id : role_ids.split(",")) {
Role _role = roleService.getById(role_id);
Role role = new Role();
role.setName(_role.getName());
role.setId(role_id);
role_list.add(role);
}
userForm.setRoles(role_list);
userValidator.validate(userForm, bindingResult, false);
if (bindingResult.hasErrors()) {
return "edit_user";
}
userService.save(userForm);
return "redirect:/users";
}
在userValidator.validate(userForm,bindingResult,false);該程序會顯示以下錯誤:
Failed to convert property value of type [java.lang.String[]] to required type [java.util.Set] for property roles; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.gestcart.account.model.Role] for property roles[0]: no matching editors or conversion strategy found
請幫助解決這個問題