Ошибка проверки Spring + Hibernate

Я пытаюсь реализовать проверку Hibernate + Spring в моем приложении, но есть одна проблема, он не будет отображать сообщение об ошибке на странице после того, как я отправляю неисправные данные. Вот код:

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;


public class StudentVO {

private int studentID;
private String userName;

@NotNull
@Size(min=2, max=45, message="Your Name should be between 2 - 45 characters.")
private String firstName;

private String middleName;

@NotNull
@Size(min=2, max=45, message="Your Surname should be between 2 - 45 characters.")
private String lastName;

JSP-страница

<form:form id="myform" modelAttribute="student" method="post"  class="form-horizontal" action="/register/registerStudent">
<div class="form-group">
<label for="firstname" class="col-sm-3 control-label">First Name</label>
    <div class="col-sm-6">
        <form:input type="text" class="form-control float_left" id="firstname" path="firstName" placeholder="First Name" />
        <form:errors path="firstName"></form:errors>
      </div>
 </div>
  <div class="form-group">
  <label for="middlename" class="col-sm-3 control-label" id="md">Middle Name</label>
      <div class="col-sm-6">
          <form:input type="text" class="form-control" id="middlename" path="middleName" placeholder="Middle Name" />
      </div>
</div>
<div class="form-group">
 <label for="lastname" class="col-sm-3 control-label">Last Name</label>
     <div class=" col-sm-6">
         <form:input type="text" class="form-control float_left"   id="lastname" path="lastName" placeholder="Last Name" required="true" />
          <form:errors path="lastName"></form:errors>
     </div>

Контроллер

@RequestMapping(value = { "/register/registerStudent" }, method = { RequestMethod.GET, RequestMethod.POST })
public String registerSubmit(@Valid StudentVO student, BindingResult result, Model model, HttpServletRequest request,
        @RequestParam String action, @RequestParam(value = "parentID", required = false) String parentID,
        @RequestParam(value = "studentID", required = false) String studentID,
        RedirectAttributes redirectAttributes) {
    if(result.hasErrors()){
        model.addAttribute("student", student);
        List<Map<String, String>> age = lookupService.getFields("age");
        model.addAttribute("age", age);
        return "studentSignup";
    }

Спасибо за помощь!

1 ответ

  1. Я нашел проблему в моем registerSubmitметоде, я не упоминал@ModelAttribute, поэтому код для метода должен выглядеть как:

    @RequestMapping(value = { "/register/registerStudent" }, method = { RequestMethod.GET, RequestMethod.POST })
    public String registerSubmit(@Valid  @ModelAttribute StudentVO student, BindingResult result, Model model, HttpServletRequest request,
        @RequestParam String action, @RequestParam(value = "parentID", required = false) String parentID,
        @RequestParam(value = "studentID", required = false) String studentID,
        RedirectAttributes redirectAttributes) {
    if(result.hasErrors()){
        model.addAttribute("student", student);
        List<Map<String, String>> age = lookupService.getFields("age");
        model.addAttribute("age", age);
        return "studentSignup";
    }