Не работает валидация полей Spring Boot
подскажите пожалуйста почему не работает валидация полей. Аннотацию на поля добавил, в контроллере в методе аннотацию @Validate указал. Не могу понять причину.
DTO класс
public class LuDTO {
@NotNull(message = "Должно быть заполнено")
private int tag;
private String shorttext;
@NotNull(message = "Должно быть заполнено")
private String text;
@NotNull(message = "Должно быть заполнено")
private String lcode;
@NotNull(message = "Должно быть заполнено")
private int code;
private LocalDate bgndat;
private LocalDate enddate;
@NotNull(message = "Должно быть заполнено")
private int status;
public int getTag() {
return tag;
}
public void setTag(int tag) {
this.tag = tag;
}
public String getShorttext() {
return shorttext;
}
public void setShorttext(String shorttext) {
this.shorttext = shorttext;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getLcode() {
return lcode;
}
public void setLcode(String lcode) {
this.lcode = lcode;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public LocalDate getBgndat() {
return bgndat;
}
public void setBgndat(LocalDate bgndat) {
this.bgndat = bgndat;
}
public LocalDate getEnddate() {
return enddate;
}
public void setEnddate(LocalDate enddate) {
this.enddate = enddate;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
}
Контроллер
@Controller
@RequestMapping("/classifier")
public class ClassifierController {
private final LuServices luServices;
private final ModelMapper modelMapper;
@Autowired
public ClassifierController(LuServices luServices, ModelMapper modelMapper) {
this.luServices = luServices;
this.modelMapper = modelMapper;
}
@GetMapping()
public String showClassifierPage(Model model) {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
PersonDetails personDetails = (PersonDetails) authentication.getPrincipal();
model.addAttribute("personDetails", personDetails);
model.addAttribute("luDTO", luServices.findByTagWith0());
model.addAttribute("newLuDTO", new LuDTO());
return "lu/index";
}
@PostMapping()
public String createLuWith0(@ModelAttribute("newLuDTO") @Valid LuDTO luDTO, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
System.out.println("****************");
StringBuilder errorMessage = new StringBuilder();
List<FieldError> errors = bindingResult.getFieldErrors();
for (FieldError error : errors) {
errorMessage.append(error.getField()).append(error.getDefaultMessage()).append(";");
}
System.out.println("****************");
return "lu/index";
} else {
luServices.addNewLu(convertToLu(luDTO));
return "redirect:/classifier";
}
}
private Lu convertToLu(LuDTO luDTO) {
return modelMapper.map(luDTO, Lu.class);
}
}
index.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:form="http://www.w3.org/1999/html">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width"/>
<link rel="stylesheet" th:href="@{/styles/css/style.css}"/>
<link rel="stylesheet" th:href="@{/styles/css/bootstrap.min.css}"/>
<link rel="stylesheet" th:href="@{/styles/css/awesome/all.css}"/>
<script th:src="@{/js/jquery.min.js}" type="text/javascript"></script>
<script th:src="@{/js/bootstrap.min.js}" type="text/javascript"></script>
<script th:src="@{/js/bootstrap.bundle.min.js}" type="text/javascript"></script>
<title>Главная страница</title>
</head>
<body>
<div class="page_wrapper">
<div class="container-fluid">
<div class="row">
<div class="col-3">
<div class="row">
<div class="item_configure_menu">
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addNewLuWith0">
<i class="fa-solid fa-house"></i>
</button>
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addNTEST">
<i class="fa-solid fa-house"></i>
</button>
</div>
<div class="lu_block">
<table>
<tr th:each="luDTO : ${luDTO}">
<td th:text="${luDTO.getText()}"></td>
<td th:text="${luDTO.getTag()}"></td>
</tr>
</table>
</div>
</div>
</div>
<div class="col-9">
test info
</div>
</div>
</div>
<div class="modal fade" id="addNewLuWith0" tabindex="-1" aria-labelledby="addNewLuWith0Label" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="addNewLuWith0Label">Добавление классификатора</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Закрыть"></button>
</div>
<div class="modal-body">
<form th:method="POST" th:action="@{/classifier}" th:object="${newLuDTO}">
<input type="text" th:field="*{tag}" id="tag" value="" placeholder="tag"/>
<div class="error_block" style="color: red" th:if="${#fields.hasErrors('tag')}" th:errors="*{tag}">tag errors</div>
<input type="text" th:field="*{shorttext}" id="shorttext" placeholder="shorttext"/>
<div class="error_block" style="color: red" th:if="${#fields.hasErrors('shorttext')}" th:errors="*{shorttext}">shorttext errors</div>
<input type="text" th:field="*{text}" id="text" placeholder="text"/>
<div class="error_block" style="color: red" th:if="${#fields.hasErrors('text')}" th:errors="*{text}">text errors</div>
<input type="text" th:field="*{lcode}" id="lcode" placeholder="lcode"/>
<div class="error_block" style="color: red" th:if="${#fields.hasErrors('lcode')}" th:errors="*{lcode}">lcode errors</div>
<input type="text" th:field="*{code}" id="code" value="" placeholder="code"/>
<div class="error_block" style="color: red" th:if="${#fields.hasErrors('code')}" th:errors="*{code}">code errors</div>
<input type="text" th:field="*{bgndat}" id="bgndat" placeholder="bgndat"/>
<div class="error_block" style="color: red" th:if="${#fields.hasErrors('bgndat')}" th:errors="*{bgndat}">bgndat errors</div>
<input type="text" th:field="*{enddate}" id="enddate" placeholder="enddate"/>
<div class="error_block" style="color: red" th:if="${#fields.hasErrors('enddate')}" th:errors="*{enddate}">enddate errors</div>
<input type="submit" value="Добавить"/>
</form>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>ru.test.myapp</groupId>
<artifactId>myapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>myapp</name>
<description>RDC MIS (Spring)</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>