gpt4 book ai didi

spring - 无法转换类型 'java.lang.String' 的属性值

转载 作者:行者123 更新时间:2023-12-05 05:19:19 25 4
gpt4 key购买 nike

我在这里做错了什么?

<select th:field="*{role}">
<option value="#" disabled="disabled" selected="selected">Role...</option>
<option th:each="r : ${roles}" th:value="${r}" th:text="${r.name}">Developer</option>
</select>

我收到这个错误:

Field error in object 'collaborator' on field 'role': rejected value
[33]; codes
[typeMismatch.collaborator.role,typeMismatch.role,typeMismatch.com.imprender.instateam.model.Role,typeMismatch];
arguments
[org.springframework.context.support.DefaultMessageSourceResolvable:
codes [collaborator.role,role]; arguments []; default message [role]];
default message [*Failed to convert property value of type
'java.lang.String' to required type
'com.imprender.instateam.model.Role' for property 'role'*; nested
exception is java.lang.IllegalStateException: Cannot convert value of
type 'java.lang.String' to required type
'com.imprender.instateam.model.Role' for property 'role': no matching
editors or conversion strategy found]

它说:

**Failed to convert property value of type 'java.lang.String' to required type 'com.imprender.instateam.model.Role' for property
'role'**

但我不明白我在做什么。

我认为我没有正确传输在 .我认为发送的对象将是在 value 选项中声明的那个被选中的对象,但显然我弄错了,找不到正确的方法。

模型:

package com.imprender.instateam.model;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;

@Entity
public class Collaborator {

@Id
//Todo: check strategy
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@NotNull
@Pattern(regexp = "([A-Z][a-zA-Z]*\\s*)+")
private String name;

//Todo: check, do we want to create a new table to associate values?

@NotNull
@ManyToOne
private Role role;

public Collaborator() {
}

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 Role getRole() {
return role;
}

public void setRole(Role role) {
this.role = role;
}
}

最佳答案

你的问题是,在你的选择中说字段是一个角色,实体类型角色,但在你的选项中,值是一个 ID,一些原始值,然后不匹配。你可以改成这个

<select th:field="*{role.id}">
<option value="#" disabled = "disabled" selected="selected">Role...</option>
<option th:each="r : ${roles}" th:value="${r.id}" th:text="${r.name}">Developer</option>
</select>

关于spring - 无法转换类型 'java.lang.String' 的属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46493428/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com