1

我开始使用Spring,我发现了一个我无法解决的问题。我有一个用户实体,具有“角色”属性,“角色”是一个集合对象,estity 如下:

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>

我选择一个或多个“角色”并将表单提交给以下控制器,其中存在一个带有所选“角色”ID列表的@RequestParam

@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

请帮忙解决这个问题

4

1 回答 1

0

您必须在用户实体中创建一个新字段作为 String[] rolesSelect 或 List < String > rolesSelect。如果您想跳过它,请将其设置为瞬态,因为它是一个实体类。然后你可以从你的模型属性“用户”访问它,你不需要单独的请求参数,即不需要'< spring:bind path =“roles” >'。在您的 jsp 上使用以下代码:

<form:select type="text" path="rolesSelect" 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>
于 2016-07-15T06:08:53.447 回答