• 1000 Я не вижу сообщений об ошибках. Я могу видеть каждое сообщение от BindingResults в режиме отладчика, я не могу понять, почему я не вижу его в моем шаблоне.
Вот моя модель и контроллер:
Модель - ->
public class UserAddBindingModel {
@NotBlank(message = "Username is required.")
private String username;
@NotBlank(message = "First Name is required.")
private String firstName;
@NotBlank(message = "Last Name is required.")
private String lastName;
public UserAddBindingModel() {
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
Контроллер ---->
@GetMapping("register")
public ModelAndView registerUser(ModelAndView modelAndView,RedirectAttributes redirectAttributes,
@ModelAttribute("userAddBindingModel") UserAddBindingModel userAddBindingModel) {
modelAndView.setViewName("auth-register");
if (modelAndView.getModel().containsKey("errors")) {
System.out.println();
}
return modelAndView;
}
@PostMapping("register")
public ModelAndView registerUserConfirm(ModelAndView modelAndView,
@ModelAttribute("userAddBindingModel") @Valid UserAddBindingModel userAddBindingModel,
BindingResult bindingResult,
RedirectAttributes redirectAttributes) {
if (bindingResult.hasErrors()) {
modelAndView.setViewName("redirect:register");
redirectAttributes.addFlashAttribute("userAddBindingModel",userAddBindingModel);
redirectAttributes.addFlashAttribute("org.springframework.validation.BindingResult.userAddBindingModel",bindingResult);
// modelAndView.addObject("errors",bindingResult.getAllErrors());
} else {
modelAndView.setViewName("redirect:/");
this.userService.registerUser(this.modelMapper.map(userAddBindingModel, UserServiceModel.class));
}
return modelAndView;
}
И, наконец, вид ->
<form th:object="${userAddBindingModel}" action="/users/register" method="POST" class="main-form mx-auto col-md-8 d-flex flex-column justify-content-center">
<div class="row">
<div class="form-group col-md-6 mb-3">
<label for="firstName" class="text-white font-weight-bold">First Name</label>
<input th:field="*{firstName}" id="firstName" type="text" min="2" max="20" class="form-control"
placeholder="First name" th:errorclass="field-error" />
<p th:if="${#fields.hasErrors('firstName')}" th:errors="*{firstName}"
class="errors alert alert-danger">
First name is required.
</p>
</div>
<div class="form-group col-md-6 mb-3">
<label for="lastName" class="text-white font-weight-bold">Last Name</label>
<input th:field="*{lastName}" id="lastName" type="text" min="2" max="20" class="form-control"
placeholder="Last name" />
<p class="errors alert alert-danger">
Last name is required.
</p>
</div>
</form>
он не отображает никаких ошибок. Я что-то упустил? Заранее спасибо :).