title: Spring MVC Validation w/ ModelAndView
body: <p>I am trying to add validation to my Spring MVC Application. Previously to attempting to set up validation I have been using ModelAndView to serve up the jsp pages but the error messages do not display.</p>
  
  <p>Model</p>
  
  <pre><code>@Entity
  @Table(name = "employee")
  public class Employee {
  
  
  @Id
  @NotEmpty(message = "Please enter your email addresss.")
  @Email
  private String email;
  
  @NotEmpty
  @Pattern(regexp="[a-zA-Z0-9]")
  private String password;
  
  @NotEmpty
  private String firstName;
  
  @NotEmpty
  private String lastName;
  
  @NotEmpty
  private String phoneNum;
  
  
  private boolean assigned;
  
  public Employee() {
  
  }
  
  // getters and setters
  }
  </code></pre>
  
  <p>Controller</p>
  
  <pre><code>@RestController
  @RequestMapping("/employee")
  public class EmployeeController {
  
  private static final Logger LOGGER = LogManager
          .getLogger(EmployeeController.class.getName());
  
  @Autowired
  private EmployeeServiceImpl employeeService;
  
  @RequestMapping(value = "/new", method = RequestMethod.GET)
  public ModelAndView getRegisterView(Model m) {
      return new ModelAndView("addEmployee", "employeeForm", new Employee());
  }
  
  @RequestMapping(value = "/register", method = RequestMethod.POST)
  public ModelAndView postUser(@Valid Employee employee, BindingResult result) {
      ModelAndView model;
      if (result.hasErrors()) {
          model = new ModelAndView("addEmployee", "employeeForm", employee);
      } else {
          employeeService.addEmployee(employee);
          model = new ModelAndView();
          model.setViewName("displayEmployee");
          model.addObject("employees", employeeService.getEmployees());
      }
  
      return model;
  }
  }
  </code></pre>
  
  <p>Form</p>
  
  <pre><code>&lt;form:form class="form-horizontal" role="form" method="POST"
          action="register" commandName="employeeForm" modelattribute="employee"&gt;
          &lt;div class="form-group"&gt;
              &lt;form:label path="firstName"
                  class="control-label col-lg-2 col-lg-offset-2 col-md-2 col-md-offset-2 col-sm-12"
                  for="fname"&gt;First Name:&lt;/form:label&gt;
              &lt;div class="col-lg-6 col-md-6 col-sm-12"&gt;
                  &lt;form:input path="firstName" class="form-control" id="fname"
                      placeholder="First Name" /&gt;
                  &lt;form:errors path="firstName" /&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
              &lt;form:label path="lastName"
                  class="control-label col-lg-2 col-lg-offset-2 col-md-2 col-md-offset-2 col-sm-12"
                  for="lname"&gt;Last Name:&lt;/form:label&gt;
              &lt;div class="col-lg-6 col-md-6 col-sm-12"&gt;
                  &lt;form:input path="lastName" class="form-control" id="lname"
                      placeholder="Last Name" /&gt;
                  &lt;form:errors path="lastName" /&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
              &lt;form:label path="email"
                  class="control-label col-lg-2 col-lg-offset-2 col-md-2 col-md-offset-2 col-sm-12"
                  for="email"&gt;Email:&lt;/form:label&gt;
              &lt;div class="col-lg-6 col-md-6 col-sm-12"&gt;
                  &lt;form:input path="email" type="email" class="form-control"
                      id="email" placeholder="Email" /&gt;
                  &lt;form:errors path="email" cssClass="error" /&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
              &lt;form:label path="phoneNum"
                  class="control-label col-lg-2 col-lg-offset-2 col-md-2 col-md-offset-2 col-sm-12"
                  for="phoneNum"&gt;Phone Number:&lt;/form:label&gt;
              &lt;div class="col-lg-6 col-md-6 col-sm-12"&gt;
                  &lt;form:input path="phoneNum" class="form-control" id="phoneNum"
                      placeholder="Phone Number" /&gt;
                  &lt;form:errors path="phoneNum" /&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
              &lt;form:label path="password"
                  class="control-label col-lg-2 col-lg-offset-2 col-md-2 col-md-offset-2 col-sm-12"
                  for="pwd"&gt;Password:&lt;/form:label&gt;
              &lt;div class="col-lg-6 col-md-6 col-sm-12 controls"&gt;
                  &lt;form:input path="password" type="password" class="form-control"
                      id="pwd" placeholder="Password" /&gt;
                  &lt;form:errors path="password" /&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class="form-group form-actions"&gt;
              &lt;div
                  class="col-lg-offset-4 col-lg-1 col-md-offset-4 col-md-1 col-sm-1"&gt;
                  &lt;button type="submit" class="btn btn-primary"&gt;Register&lt;/button&gt;
              &lt;/div&gt;
              &lt;div class="col-lg-1 col-md-1 col-sm-1"&gt;
                  &lt;button type="reset" class="btn btn-primary"&gt;Clear&lt;/button&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;form:hidden path="assigned" value="false" /&gt;
      &lt;/form:form&gt;
  </code></pre>