I'm in the process of learning spring. I'm using thymeleaf to create a simple web app that adds, edits and removes users from a database.
I'm using an html page to display the database and two separate pages for editing and adding new users.
Edit and remove work perfectly but whenever i try to add a user i get an error in new.html (new.html contains the form to add a new user)
Property or field xxxx cannot be found on null
The error shows up in the from at th:text="@{user.name}"
.From what I've found online thymelaf does not take null values, however as this is a new object I am trying to add all values are null.
Is there any way to solve this. Code.
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>
Controller
@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";
}
Service class
@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());
}
}
Users (Model)
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;
}
}