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

Bean 类的属性 'id' 无效:Bean 属性 'id' 不可读或具有无效的 getter 方法:getter 的返回类型是否匹配

谭向晨
2023-03-14

现在在用spring框架spring boot version = ' 1 . 5 . 9 . release '做java。但是每次我运行我的网络应用程序

org.springframework.beans.NotReadable属性异常: bean类的无效属性'id'[java.util.ArrayList]: Bean属性'id'不可读或具有无效的getter方法:getter的返回类型是否与setter的参数类型相匹配?在org.springframework.beans.AbstractNestablePropertyAccessor.get属性值(AbstractNestablePropertyAccessor.java:631)~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]在org.springframework.beans.AbstractNestablePropertyAccessor.get属性值(AbstractNestablePropertyAccessor.java:622)~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]在org.springframework.web.servlet.support.BindState.(BindStatus.java:149)~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE]在org.thymeleaf.spring4.util.FieldUtils.getBindStusFromParsed表达式(FieldUtils.java:401)~[thymeleaf-spring4-2.1.6.RELEASE.jar:2.1.6.RELEASE]

我的用户类是

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class User {

@Id
private int id;
private String name;
private int age;

public User() {
}

public User(int id, String name, int age) {
    this.id = id;
    this.name = name;
    this.age = age;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}
}

我的控制器类是

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class UserController {

@Autowired
private UserService userService;

@RequestMapping(value = "/")
public String display(ModelMap modelMap){
    User user = new User(1, "Endriyas", 21);
    userService.addUser(user);
    List<User> users = userService.getAllUser();
    modelMap.put("users", users);
    return "users";
}

}

我的 HTML 表单是

<div>
    <form action="#" th:action="@{/}" th:object="${users}" method="post">
        <div>
            <label>
                <span>ID</span>
            </label>
            <input type="number" th:field="*{id}"/>
        </div>
        <div>
            <label>
                <span>Name</span>
            </label>
            <input type="text" th:field="*{name}"/>
        </div>
        <div>
            <label>
                <span>Age</span>
            </label>
            <input type="number" th:field="*{age}"/>
        </div>
        <div>
            <button type="submit" name="save">SAVE</button>
        </div>
    </form>
</div>

我需要帮助

谢谢

共有1个答案

甘明朗
2023-03-14

您传递的是一个用户列表(在控制器中),但实际上是要保存一个用户的表单。

为了仅显示新创建的用户(使用默认值)

将代码更改为

 @RequestMapping(value = "/")
public String display(ModelMap modelMap){
    User user = new User(1, "Endriyas", 21);
    userService.addUser(user);
    User users = userService.getById(1);
    modelMap.put("users", users);
    return "users";
}

此外,您应该在服务中创建一个新方法

getById (that has an Integer as a parameter)

addUser方法是否将用户保存在数据库中?如果不是,您只想用一些默认值显示用户,请将代码更改为

@RequestMapping(value = "/")
    public String display(ModelMap modelMap){
        User user = new User(1, "Endriyas", 21);
        modelMap.put("users", user);
        return "users";
    }
 类似资料: