[无法将类型'java.lang.String []'的属性值转换为属性

时间:2017-10-04 16:58:02

标签: java spring spring-mvc thymeleaf

我正在使用Thymeleaf并尝试进行一些对象绑定,但如果我有一个带有列表的对象,我不知道该怎么做。让我解释一下:

我的模特:

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

我收到错误:

  

错误!!!!:字段'rolesNeeded'上的对象'项目'中的字段错误:   被拒绝的价值   [com.imprender.instateam.model.Role @ 145d6cd4,com.imprender.instateam.model.Role @ 73020d6f];   代码   [typeMismatch.project.rolesNeeded,typeMismatch.rolesNeeded,typeMismatch.java.util.List,typeMismatch];   参数   [org.springframework.context.support.DefaultMessageSourceResolvable:   代码[project.rolesNeeded,rolesNeeded];参数[];默认消息   [rolesNeeded]];默认消息[无法转换属性值   输入'java.lang.String []'到必需类型'java.util.List'   属性'rolesNeeded';嵌套异常是   java.lang.IllegalStateException:无法转换类型的值   'java.lang.String'到必需的类型   'com.imprender.instateam.model.Role'属性'rolesNeeded [0]':否   找到匹配的编辑器或转换策略]

基本上,据我所知,复选框输入返回一个String [],但我的对象需要一个列表,因此无法执行绑定。

我如何在列表中绑定数组?(你有一个例子吗?)

谢谢。

1 个答案:

答案 0 :(得分: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填充它们。