0

私はSpringで作業を始めました。私は分類できない問題を発見しました。 私は「役割」、「役割」プロパティで、Userエンティティを持っているが、設定されたオブジェクトで、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; 
} 
私は、私は1つまたは多くの「役割」を選択し、コントローラを次のようにフォームを提出するすべての「役割」

<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> 

のリストとコンボボックスを持っているJSPでは

、この内の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 

をしてください、あなたは[] Stringとして、ユーザーエンティティにrolesSelectまたは一覧<文字列> rolesSelectを新しいフィールドを作成するために必要があり、この問題

答えて

0

を解決するのに役立ちます。エンティティクラスであるため、スキップしたい場合は一時的にします。それから、モデル属性 "user"からアクセスできるようになります。リクエストパラメータは別に必要です。つまり、 '< 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> 
関連する問題