在数据库中插入新项目的Thymeleaf生成错误在null

时间:2017-08-30 11:28:04

标签: spring-mvc thymeleaf

我正在学习春天的过程。我使用百万美元来创建一个简单的Web应用程序,用于添加,编辑和删除数据库中的用户。

我使用html页面显示数据库和两个单独的页面,用于编辑和添加新用户。

完美地编辑和删除工作,但每当我尝试添加用户时,我在new.html中收到错误(new.html包含添加新用户的表单)

Property or field xxxx cannot be found on null

错误显示在th:text="@{user.name}"处。我在网上发现的百里香不会取空值,但是因为这是我想要添加的新对象所有值都为空。

有没有办法解决这个问题。代码。

new.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>New User</title>
</head>
<body>
<form method="post" name="comment_form" id="comment_form" th:action="@{/create}" role="form">
    Name:<br>
    <input type="text" name="name" th:text="${user.name}"><br>
    Age:<br>
    <input type="text" name="age" th:text="${user.age}"><br>
    Email: <br>
    <input type="text" name="email" th:text="${user.email}"><br>
    <button type="submit" id="submit" class="btn btn-primary">Submit</button>
</form>
</body>
</html>

控制器

    @Autowired
UserService service;
    @RequestMapping(value="user/new", method = RequestMethod.GET)
public String newUser(Long id, Model md) {
    Users user = service.findOne(id);
    md.addAttribute("user", user);
    return "new";
}

@RequestMapping(value = "/create", method = RequestMethod.POST)
public String create(@RequestParam("id") Long id, @RequestParam("name") String name, @RequestParam("age") int age,
                     @RequestParam("email") String email, Model md) {
    md.addAttribute("users", service.addOrUpdate(new Users(id, name, age)));
    return "redirect:/user";
}

服务类

@Autowired
JdbcTemplate template;
public Users findOne(Long id)
{
    String sql = "select * from people where id=" +id;
    return template.query(sql, new ResultSetExtractor<Users>() {
        @Override
        public Users extractData(ResultSet resultSet) throws SQLException, DataAccessException {
            if (resultSet.next()) {
                Users user = new Users(resultSet.getLong("id"),
                        resultSet.getString("name"),
                        resultSet.getInt("age"));
                String email = resultSet.getString("email");
                if (email != null) {
                    user.setEmail(email);
                }
                return user;
            }
            return null;
        }
    });


}

public int addOrUpdate(Users user){
        if (user.getId() > 0) {
            String sql = "UPDATE people SET name=?, age =?, email=? WHERE id=" +user.getId();
            System.out.println(sql);
            return template.update(sql, user.getName(), user.getAge(), user.getEmail());

        } else {
            String sql = "INSERT INTO people ( name, age, email) VALUES ( ?, ?, ?)";
            System.out.println(sql);
            return template.update(sql, user.getName(), user.getAge(), user.getEmail());

        }
}

用户(型号)

package ro.database.jdbcTest.model;


import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

public class Users {

@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private Long id;
private String name;
private int age;
private String email;


public Long getId() {
    return id;
}

public String getName() {
    return name;
}

public int getAge() {
    return age;
}

public String getEmail() {
    return email;
}

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

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

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

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

public void setEmail(String email){
    this.email=email;
}
}

1 个答案:

答案 0 :(得分:1)

由于您的user对象为null,您将收到该错误。

您需要做的就是每次向新用户发出请求时都会发送new User()对象。

@RequestMapping(value="user/new", method = RequestMethod.GET)
public String newUser(Long id, Model md) {
    Users user = null;
    if(id > 0) { // Id is present, fetch from the database.
        user = service.findOne(id);
    } else { // Create a new user.
        user = new User();
    }
    md.addAttribute("user", user);
    return "new";
}

使用上述方式,null

中永远不会有new.html个用户