当前位置: 首页 > 知识库问答 >
问题:

[无法将类型“java.lang.String[]”的属性值转换为属性所需的类型“java.util.List”

贺宏逸
2023-03-14

我正在使用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;
    }
}
<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>

错误!!!!:对象“Project”中字段“Roles Needed”上的字段错误:拒绝值[com.imprender.instateam.model.role@145d6cd4,com.imprender.instateam.model.role@73020d6f];代码[TypeMismatch.project.RolesNeed,TypeMismatch.RolesNeed,TypeMismatch.java.util.List,TypeMismatch];参数[org.springframework.context.support.DefaultMessageSourceResolvable:代码[project.rolesNeed,rolesNeed];参数[];默认消息[rolesNeed]];默认消息[未能将属性“Roles Needed”的类型“java.lang.String[]”的属性值转换为必需的类型“java.util.List”;嵌套异常是java.lang.IllegalStateException:无法将属性“Roles Need[0]”的类型“java.lang.String”的值转换为必需的类型“com.imprender.Instateam.model.Role”:找不到匹配的编辑器或转换策略]

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

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

谢谢你。

共有1个答案

党建义
2023-03-14

如果您的角色bean具有activeboolean属性,则可以执行以下操作(简化):

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

如果没有,您可以将roleSneed存储在隐藏字段中,并用JavaScript填充它们。

 类似资料: