0

나는 Spring과 작업하기 시작했고, 나는 정렬 할 수없는 문제를 발견했다.Spring MVC - 컨트롤러의 @ModelAttribute 객체에 대한 속성 설정

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; 
} 
:

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

내 '역할'기업은 다음과 같다 : 가 나는 '역할'속성으로, 사용자 개체가, '역할'은 설정 개체가의 estity는 다음과 는 JSP에서

나는 목록과 콤보를 모두 '역할'나는 식별자의 목록이있는 @RequestParam 존재 하나 또는 여러 '역할'을 선택하고이에, 다음과 같은 컨트롤러에 양식을 제출

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

선택한 '역할'중

@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 

, 당신은 문자열 [] 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>