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

时间:2018-03-21 16:01:46

标签: java spring-boot thymeleaf javabeans

现在使用springframework springBootVersion =' 1.5.9.RELEASE' 但每次我运行我的网络应用程序它给我

org.springframework.beans.NotReadablePropertyException:无效的属性' id' bean类[java.util.ArrayList]:Bean属性' id'不可读或getter方法无效:getter的返回类型是否与setter的参数类型匹配?     在org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:631)〜[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]     在org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:622)〜[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]     在org.springframework.web.servlet.support.BindStatus。(BindStatus.java:149)〜[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE]     在org.thymeleaf.spring4.util.FieldUtils.getBindStatusFromParsedExpression(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 个答案:

答案 0 :(得分:0)

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

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

将代码更改为

 @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";
    }