title: Returning errors from controller back to view in Spring MVC
body: <p>In Spring MVC I have the following controller:</p>
  
  <pre><code>@RequestMapping(value="adminUsers", method = RequestMethod.POST) 
  public ModelAndView listAdminUsers(Person newPerson,  HttpServletResponse response)  throws Exception {
  Person person = personService.findPerson(newPerson.getUsername());
  if (person == null) {
      // Set errorText = "Invalid Person";
      // redisplay view
      ModelAndView mav = new ModelAndView("adminUsersList");
      return mav;
  } else {
      Roles roles = new Roles();
      roles.setPersonCode(person.getPersonCode());
      roles.setRoleType("ADMN");
      rolesMapper.insert(roles);
      return new ModelAndView("redirect:/admin/adminUsers.html");
  } 
  }
  </code></pre>
  
  <p>With the view:</p>
  
  <pre><code>    &lt;form:form method="post" action="${action}" commandName="person" &gt;
      &lt;form:label path="username"&gt;Add new administrator:&lt;/form:label&gt;
      &lt;form:input path="username" size="20"/&gt;
      &lt;form:errors path="username" /&gt;
      &lt;input type="submit" value="Submit Changes"/&gt; &lt;/form:form&gt;
  </code></pre>
  
  <p>How would I return an error back to the view, so it is displayed by the <code>&lt;form:errors path="username" /&gt;</code> tag?</p>
  
  <p>This is simlar to <a href="https://stackoverflow.com/questions/9337824/how-to-return-error-status-and-validation-errors-from-this-spring-mvc-controller">How to return error status and validation errors from this Spring MVC controller?</a> except I'm returning a webpage, not a REST object, so the answers there don't apply.</p>
  
  <p>I don't want to use <code>Validator</code> because the above calls the database, it's not simply a check for if username is not empty etc.</p>
  
  <p>Thanks!</p>