Валидация данных Spring MVC
Пытаюсь сделать валидацию полей из формы.
В контроллере прописал что если при валидации обнаружены ошибки должен быть переход на страницу
localhost:8080/people/new
но вместо этого переход идет на страницу
localhost:8080/people
Подскажите пожалуйста в чем ошибка?
Веб форма:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>New person</title>
</head>
<body>
<form th:method="POST" th:action="@{/people}" th:object="${person}">
<input type="text" th:field="*{name}" placeholder="Name"/>
<div style="color: red" th:if="${#fields.hasErrors('name')}" th:onerror="*{name}"></div> <br/>
<input type="text" th:field="*{age}" placeholder="Age"/>
<div style="color: red" th:if="${#fields.hasErrors('age')}" th:errors="*{age}"></div> <br/>
<input type="text" th:field="*{email}" placeholder="E-mail"/>
<div style="color: red" th:if="${#fields.hasErrors('email')}" th:errors="*{email}"></div> <br/>
<hr/>
<input type="submit" value="Add person"/>
</form>
</body>
</html>
Модель Person
import org.hibernate.validator.constraints.NotEmpty;
import javax.validation.constraints.Email;
import javax.validation.constraints.Min;
import javax.validation.constraints.Size;
public class Person {
private int id;
@NotEmpty(message = "Not null")
@Size(min = 3, max = 50, message = "max size 50 symbols")
private String name;
@Min(value = 0, message = "min age 0")
private int age;
@Email(message = "no format")
@NotEmpty(message = "Not null")
private String email;
public Person() {
}
public Person(int id, String name, int age, String email) {
this.id = id;
this.name = name;
this.age = age;
this.email = email;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
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;
}
}
Контроллер
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import ru.test.spring_mvc_crud.dao.PersonDAO;
import ru.test.spring_mvc_crud.models.Person;
@Controller
@RequestMapping("/people")
public class PeopleController {
private PersonDAO personDAO;
public PeopleController(PersonDAO personDAO) {
this.personDAO = personDAO;
}
@GetMapping
public String index(Model model) {
//Получаем людей из DAO и выводим
model.addAttribute("people", personDAO.index());
return "people/index";
}
@GetMapping("/{id}")
public String show(@PathVariable("id") int id, Model model) {
//Получим человека по id из DAO
model.addAttribute("person", personDAO.show(id));
return "people/show";
}
@GetMapping("/new")
public String newPerson(@ModelAttribute("person") Person person) {
return "people/new";
}
@PostMapping()
public String create(@ModelAttribute("person") @Valid Person person, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
return "people/new";
} else {
personDAO.save(person);
return "redirect:/people"; //редирект
}
}
@GetMapping("/{id}/edit")
public String edit(@PathVariable("id") int id, Model model) {
model.addAttribute("person", personDAO.show(id));
return "people/edit";
}
@PatchMapping("/{id}")
public String update(@ModelAttribute("person") @Valid Person person, BindingResult bindingResult, @PathVariable("id") int id) {
if (bindingResult.hasErrors()) {
return "people/edit";
}
personDAO.update(id, person);
return "redirect:/people";
}
@DeleteMapping("/{id}")
public String delete(@PathVariable("id") int id) {
personDAO.delete(id);
return "redirect:/people";
}
}
В pom.xml
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.2.3.Final</version>
</dependency>