Thymeleaf와 협력 중이며 일부 개체 바인딩을 시도하고 있지만 잘 모릅니다. 내가 목록이있는 객체가 있다면 어떻게 할 것인가. 설명해 드리죠 :[java.lang.String [] '유형의 속성 값을 속성'java.util.List '의 필수 유형으로 변환하지 못했습니다.
내 모델 :
@Entity
public class Project {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotNull
private String name;
@NotNull
@Lob
private String description;
@NotNull
private Date startDate;
private String status;
@ManyToMany
private List<Role> rolesNeeded;
@ManyToMany
private List<Collaborator> collaborators;
public Project() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public List<Role> getRolesNeeded() {
return rolesNeeded;
}
public void setRolesNeeded(List<Role> rolesNeeded) {
this.rolesNeeded = rolesNeeded;
}
public List<Collaborator> getCollaborators() {
return collaborators;
}
public void setCollaborators(List<Collaborator> collaborators) {
this.collaborators = collaborators;
}
}
내 HTML 양식 :
<form method="post" action="addproject" th:object="${project}">
<div>
<label for="project_name"> Project Name:</label>
<input th:field="*{name}" type="text" name="project_name"/>
</div>
<div>
<label for="project_description">Project Description:</label>
<textarea th:field="*{description}" rows="4" name="project_description"></textarea>
</div>
<div>
<label for="project_status">Project Status:</label>
<div class="custom-select">
<span class="dropdown-arrow"></span>
<select th:field="*{status}" name="project_status">
<option value="active">Active</option>
<option value="archived">Archived</option>
<option value="not_started">Not Started</option>
</select>
</div>
</div>
<div>
<label for="project_roles">Project Roles:</label>
<ul class="checkbox-list">
<li th:each="role : ${roles}">
<input th:field="*{rolesNeeded}" type="checkbox" name="project_roles" th:value="${role}"/>
<span class="primary" th:text="${role.name}"> Developer</span>
</li>
</ul>
</div>
<div class="actions">
<input type="submit" value="Save" class="button"/>
<a href="#" class="button button-secondary">Cancel</a>
</div>
</form>
그리고 오류 얻고있다 : 내가 이해로까지, 기본적으로
ERROR!!!!: Field error in object 'project' on field 'rolesNeeded': rejected value [[email protected],[email protected]]; codes [typeMismatch.project.rolesNeeded,typeMismatch.rolesNeeded,typeMismatch.java.util.List,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [project.rolesNeeded,rolesNeeded]; arguments []; default message [rolesNeeded]]; default message [Failed to convert property value of type 'java.lang.String[]' to required type 'java.util.List' for property 'rolesNeeded'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'com.imprender.instateam.model.Role' for property 'rolesNeeded[0]': no matching editors or conversion strategy found]
을 , 체크 박스 입력은 String []을 반환하지만, 바인딩이 이루어질 수 없도록 내 객체는리스트를 필요로합니다.
목록의 배열을 어떻게 바인딩 할 수 있습니까? (예가 있습니까?)
고맙습니다. 그렇지 않은 경우
<ul class="checkbox-list">
<li th:each="role,stat : ${project.rolesNeeded}">
<input th:field="*{rolesNeeded[__${stat.index}__].active}" type="checkbox"/>
<input th:field="*{rolesNeeded[__${stat.index}__].name}" type="hidden"/>
<span class="primary" th:text="${role.name}">Developer</span>
</li>
</ul>
, 당신은 숨겨진 필드에 rolesNeeded
를 저장하고 자바 스크립트로 채울 수 : 귀하의 Role
콩이 active
부울 속성이있는 경우