title: org.springframework.validation.BeanPropertyBindingResult
body: <p>Controller class-</p>
  
  <pre><code>@Controller
  @SessionAttributes({"id", "roleId"})
  @RequestMapping("/User")
  public class UserController {
  
  @Autowired
  private UserService userv = null;           
  
  @InitBinder("stdUser")
  private void initBinder(WebDataBinder binder) {
      System.out.println("1111======"+binder.getObjectName());
      binder.setValidator(new NewUserValidator());
      System.out.println("2222======"+binder.getObjectName());
  }
  
  @RequestMapping(value = "/allUsers")
  public ModelAndView allUser(@ModelAttribute("userSetup")@Valid UserBean stdUser, BindingResult result, Map&lt;String, Object&gt; map, HttpSession session) {
      StdCheckAccessV chk = new StdCheckAccessV();
      chk.setFexe(Screens.User.substring(Screens.User.lastIndexOf("/") + 1, Screens.User.length()));
      chk.setUid(Long.parseLong(session.getAttribute("id").toString().trim()));
      chk.setRid(Long.parseLong(session.getAttribute("roleId").toString().trim()));
      chk = userv.getAccess(chk);
      List&lt;StdUsers&gt; l = userv.getUsersList();
      stdUser.setChkAccessV(chk);
      map.put("userList", l);
      return new ModelAndView(Screens.User);
  }
  @RequestMapping(value = "/submitUser")
  public ModelAndView addOrUpdate(@ModelAttribute("userSetup")@Valid UserBean stdUser, BindingResult result, HttpSession session, final RedirectAttributes redA) {
      try {
          int res = 0;
          Long id = stdUser.getStdUsers().getId();
          if (result.hasErrors()) {
              System.out.println("///////result has errors   " + result.toString());
              return new ModelAndView("redirect:/User/allUsers");
          }
          System.out.println("////////the id============"+id);
          if (id == null) {
              System.out.println("/////inside the if");
              stdUser.getStdUsers().setUserGroupId(Long.parseLong(session.getAttribute("id").toString().trim()));
              stdUser.getStdUsers().setCreatedBy(Long.parseLong(session.getAttribute("id").toString().trim()));
              stdUser.getStdUsers().setCreationDate(new Date());
              res = userv.submitUser(stdUser);
          } else {
              System.out.println("/////inside the else");
              stdUser.getStdUsers().setUpdateDate(new Date());
              stdUser.getStdUsers().setUpdatedBy(Long.parseLong(session.getAttribute("id").toString().trim()));
              res = userv.updateUser(stdUser);
          }
      } catch (Exception e) {
          System.out.println("////Exception in add or update method " + e);
      }
      return new ModelAndView("redirect:/User/allUsers");
      //return "redirect:/User/allUsers";
  }}
  </code></pre>
  
  <p>Validator class-</p>
  
  <pre><code>@Component
  public class NewUserValidator implements Validator {
  
  @Override
  public boolean supports(Class&lt;?&gt; userOb) {
      return UserBean.class.equals(userOb);
  }
  
  @Override
  public void validate(Object target, Errors errors) {
      try {
          UserBean user = (UserBean) target;
          if (user.getStdUsers().getUserName() == null) {
              ValidationUtils.rejectIfEmptyOrWhitespace(errors, "stdUsers.userName", "user.name.empty");
              //errors.reject("stdUsers.userName", "User Name is mandatory");
          }
      } catch (Exception e) {
          System.out.println(e);
      }
  }
  </code></pre>
  
  <p>}
  Bean Class-</p>
  
  <pre><code>public class UserBean {
  
  private @Valid StdUsers stdUsers;
  private @Valid StdCheckAccessV chkAccessV;
  private boolean listView = true;
  private String rePassword;
  
  getters and setters.... }
  </code></pre>
  
  <p>index.jsp-</p>
  
  <pre><code>&lt;body&gt;
      &lt;%
          session.setAttribute("id", 1);
          session.setAttribute("roleId", 1);
      %&gt;
      &lt;a href="User/allUsers.html"&gt;User&lt;/a&gt;&lt;br&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p>CMN/STD100002.jsp page</p>
  
  <pre><code>&lt;td class="tdright"&gt;&lt;form:label path="stdUsers.userName"&gt;User Name&lt;em&gt;*&lt;/em&gt;&lt;/form:label&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;form:input path="stdUsers.userName"/&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;form:errors path="stdUsers.userName"&gt;&lt;/form:errors&gt;&lt;/td&gt;
  &lt;a href="#" name="btnsub" id="btnsub" onclick="submitUser()"&gt;
                                  &lt;table cellspacing="0" cellpadding="0" border="0"&gt;
                                      &lt;tr&gt;
                                          &lt;td width="25" height="24px" align="center"&gt;
                                              &lt;img src="${pageContext.servletContext.contextPath}/resources/images/Buttons/gray icon/Add.png" width="16" height="15"  border="0" /&gt;
                                          &lt;/td&gt;
                                          &lt;td&gt;Submit&lt;/td&gt;
                                          &lt;td&gt;&lt;/td&gt;
                                      &lt;/tr&gt;
                                  &lt;/table&gt;
                              &lt;/a&gt;
          &lt;script type="text/javascript"&gt;
  
          function submitUser(){
              document.form1.action="${pageContext.servletContext.contextPath}/User/submitUser";
              document.form1.submit();
          }
  
      &lt;/script&gt;
  </code></pre>
  
  <p>I am following a tutorial for using validation in spring as i am new for spring.
  On click of the submit button while leaving the <strong>User Name</strong> field empty, it should get validated with the message from Validator.
  When i tried to print the error in controller class by-</p>
  
  <pre><code>System.out.println("result has errors   " + result.toString());
  </code></pre>
  
  <p>it prints-</p>
  
  <pre><code>result has errors   org.springframework.validation.BeanPropertyBindingResult: 1 errors
  </code></pre>
  
  <p>Please help, i am not getting where i am wrong.</p>
  
  <p>I used this code to check where is the error-</p>
  
  <pre><code>for (Object object : result.getAllErrors()) {
              if (object instanceof FieldError) {
                  FieldError fieldError = (FieldError) object;
                  System.out.println("the field errors::::::::::::::::"+fieldError.getCode());
              }
              if (object instanceof ObjectError) {
                  ObjectError objectError = (ObjectError) object;
                  System.out.println("the object errors:::::::::"+objectError.getCode());
              }
          }
  </code></pre>
  
  <p>and i found-</p>
  
  <pre><code>the field errors::::::::::::::::user.name.empty
  the object errors:::::::::user.name.empty
  ///////result has errors   org.springframework.validation.BeanPropertyBindingResult: 2       errors
  
  Field error in object 'userSetup' on field 'stdUsers.userName': rejected value []; codes  [user.name.empty.userSetup.stdUsers.userName,user.name.empty.stdUsers.userName,user.name.em pty.userName,user.name.empty.java.lang.String,user.name.empty]; arguments []; default  message [null]
  </code></pre>
  
  <p>Since these two fields i am putting empty to check validation. If i will get these error on putting the fields empty then how can i validate them?</p>
  
  <p>StdUser-</p>
  
  <pre><code>@Entity
  @Table(name = "STD_USERS")
  @NamedQueries({
  @NamedQuery(name = "StdUsers.findAll", query = "SELECT s FROM StdUsers s")})
  public class StdUsers implements Serializable {
  private static final long serialVersionUID = 1L;
  @Id
  @GenericGenerator(name="generator",strategy="increment")
  @GeneratedValue(generator="generator")
  @Basic(optional = false)
  @Column(name = "ID", nullable = false)
  private Long id;
  @Basic(optional = false)
  @Column(name = "USER_NAME", nullable = false, length = 50)
  private String userName;
  
  setters and getters.....
  </code></pre>
  
  <p>Any one please reply.</p>