title: Spring MVC -Is there alternative approach to display form validation error message (without using @ModelAtrribute along with @Valid)?
body: <p>Trying to get Spring annotation based validation working in a simple webapp. While using @ModelAttribute along with @Valid, I am getting validation error next to the field, however when I am adding object to model, not getting error messages. </p>
  
  <p>Suggest any alternative approach to display form validation error message(i.e. without using @ModelAtrribute along with @Valid)</p>
  
  <p>Below is the code:</p>
  
  <p>EmployeeModel.java</p>
  
  <pre><code>@Entity
  @Table(name = "Employee")
  public class EmployeeModel implements Serializable {
  
      /**
       *
       */
      private static final long serialVersionUID = 1L;
  
      @Id
      @Column(name = "ID")
      @NotNull(message = "ID is required")
      private Integer employee_id;
  
      @Column(name = "Name")
      @NotEmpty(message = "Name is required")
      private String employee_name;
  
      @Column(name = "Manager")
      private String manager;
  
      public Integer getEmployee_id() {
          return employee_id;
      }
  
      public void setEmployee_id(Integer employee_id) {
          this.employee_id = employee_id;
      }
  
      public String getEmployee_name() {
          return employee_name;
      }
  
      public void setEmployee_name(String employee_name) {
          this.employee_name = employee_name;
      }
  
      public String getManager() {
          return manager;
      }
  
      public void setManager(String manager) {
          this.manager = manager;
      }
  
      @Override
      public String toString() {
          return "Employee [id=" + employee_id + ", Name=" + employee_name
                  + ", Manager=" + manager + "]";
      }
  }
  </code></pre>
  
  <p>Code snippet in Controller</p>
  
  <pre><code>@RequestMapping(value = "/save", method = RequestMethod.POST)
  public String saveEmployee(Model model, @Valid EmployeeModel employee, BindingResult result) {
      if (result.hasErrors()) {
          model.addAttribute("employee", employee);
          //model.addAttribute(employee);
          return "EmployeeForm";
      }
      employeeService.addEmployee(employee);
      return "forward:/";
  }
  </code></pre>
  
  <p>View:</p>
  
  <pre><code>&lt;form:form action="save" method="post" modelAttribute="employee"&gt;
                  &lt;tr&gt;
                      &lt;td&gt;&lt;form:label path="employee_id"&gt;Employee ID:&lt;/form:label&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;form:input path="employee_id" /&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;form:errors path="employee_id"/&gt;&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td&gt;&lt;form:label path="employee_name"&gt;Employee Name:&lt;/form:label&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;form:input path="employee_name" /&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;form:errors path="employee_name"/&gt;&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td&gt;&lt;form:label path="manager"&gt;Manager:&lt;/form:label&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;form:input path="manager" /&gt;&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td colspan="2" align="center"&gt;&lt;input type="submit"
                          value="Save"&gt;&lt;/td&gt;
                  &lt;/tr&gt;
              &lt;/form:form&gt;
  </code></pre>
  
  <p>In case I use the commented line</p>
  
  <pre><code>//model.addAttribute(employee)
  </code></pre>
  
  <p>I get exception stating: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'employee' available as request attribute.</p>
  
  <p>Replacing the commented line with</p>
  
  <pre><code>model.addAttribute("employee",employee)
  </code></pre>
  
  <p>Page gets loaded with the previous value entered which means invalid object is not getting replaced, still error message is not displayed.</p>
  
  <p>The above can be achieved using @ModelAttribute along with @Valid, but is there any other alternative i.e. without using @ModelAtrribute?</p>
  
  <p>What is the difference in working with @ModelAttribute and Model, as till now I considered both works in similar way?</p>