title: How to show error message from implemented custom validator in JSP view?
body: <p>I am writing a custom validator. It should just compare 2 field values in the form and reject if they are same. It successfully rejects, but I can not send my error message to the view. </p>
  
  <p>This is my custom validator class:</p>
  
  <pre><code>
  public class CheckSameNameValidator implements Validator {
  
      @Override
      public boolean supports(Class&lt;?&gt; clazz) {
          // TODO Auto-generated method stub
          return FormModel.class.equals(clazz);
      }
  
      @Override
      public void validate(Object target, Errors errors) {
          FormModel model = (FormModel) target;
          if (model.getPerson1().getName().equals(model.getPerson2().getName())) {
              System.out.println("error occurred");
              errors.reject("person can not relate to himself!");
          }
      }
  
  }
  </code></pre>
  
  <p>This is the FormModel class used in validation:</p>
  
  <pre><code>public class FormModel {
      private Person person1;
      private Person person2;
  
      public Person getPerson1() {
          return person1;
      }
  
      public void setPerson1(Person person1) {
          this.person1 = person1;
      }
  
      public Person getPerson2() {
          return person2;
      }
  
      public void setPerson2(Person person2) {
          this.person2 = person2;
      }
  }
  </code></pre>
  
  <p>This is the controller method used:</p>
  
  <pre><code>@RequestMapping(value = "/setRelative", method = RequestMethod.POST)
      public ModelAndView setRelative(@Valid @ModelAttribute("people") FormModel people, BindingResult bindingResult,
              HttpServletRequest request) {
          logger.info("set relative controller");
          CheckSameNameValidator validator = new CheckSameNameValidator();
          validator.validate(people, bindingResult);
          if (bindingResult.hasErrors()) {
              ModelAndView model = new ModelAndView();
              model.setViewName("index");
              model.addObject("people", people);
              return model;
          } else {
    }
  }
  </code></pre>
  
  <p>And this is the form in jsp:</p>
  
  <pre><code>&lt;f:form class="form-inline" action="setRelative"
      modelAttribute="people"&gt;
  
      &lt;label&gt;Person:&lt;/label&gt;
      &lt;f:select cssStyle="width:150px" path="person1.name" items="${nameList}"
              multiple="false"&gt;
          &lt;/f:select&gt; 
          &lt;f:errors path="person1.name" class="alert alert-danger"&gt;&lt;/f:errors&gt;
  
      &lt;f:label path="person2.name"&gt;Relative:&lt;/f:label&gt;
      &lt;f:select cssStyle="width:150px" path="person2.name" items="${nameList}"
              multiple="false"&gt;
          &lt;/f:select&gt; 
          &lt;f:errors path="person2.name" class="alert alert-danger"&gt;&lt;/f:errors&gt;
  
          &lt;label&gt;Person's Relation to Relative:&lt;/label&gt;
      &lt;f:select cssStyle="width:150px" path="person2.relations"
          items="${relationList}" multiple="false"&gt; 
      &lt;/f:select&gt;
      &lt;button class="btn btn-primary" type="submit"&gt;Set
          relative&lt;/button&gt;
  
  &lt;/f:form&gt;
  </code></pre>
  
  <p>I expect the error message to be printed to the view, if error occurred. How to achieve it?</p>