2017-10-04 1017 views
0

我正在與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 [],但是我的對象需要一個列表,所以綁定不能被執行。

我該如何綁定列表中的數組?(你有沒有例子?)

謝謝。

回答

0

如果你Role bean有active布爾屬性,你可以做這樣的事情(簡體):

<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存儲在隱藏字段,並用JavaScript填充它們。

相關問題