Spring Validator - show errors using <form:errors

I am working in a spring mvc web app where i am trying to validate an db object person using Spring's Validator and trying to show the result of validator in JSP form as form:errors path="". I have server side method in a controller as :

@RequestMapping( value="/find", method = RequestMethod.GET )
public ModelAndView search(@ModelAttribute("Person") Person p,BindingResult result){

     Person person2 = personDao.get( p.getId() );
     ModelAndView mav = new ModelAndView("templates/person");

     PersonValidator personValidator = new PersonValidator();
     personValidator.validate(person2, result);
     mav.addObject("person",person2);
     mav.addObject("errors",result.getFieldErrors());

     return mav;
}

I have JSP code as :

<form:form commandName="person" >
    <form:input path="personEmail" /> 
    <form:errors path="personEmail"  element="div" />
</form:form>

It works if I explicitly put the error message ${errors.personEmail }

But ,

<form:errors path="personEmail"  element="div" />

does not work.

Could you please suggest me what's wrong here?

Answers


I did the following changes and it is solved now. :D

@RequestMapping( value="/find", method = RequestMethod.GET )
    public String search(@ModelAttribute("Person") Person p,BindingResult result,ModelMap model){

         Person person2 = personDao.get( p.getId() );
         PersonValidator personValidator = new PersonValidator();
         Errors errors = new BeanPropertyBindingResult(person2, "person");
         personValidator.validate(person2, errors);
         model.addAttribute("person",person2);
         model.addAttribute("errors",errors);

         return "templates/person";
 }

please try to narrow variable names.

You have got @Model Attribute("Person") where the command is named Person than in your jsp you have person and in personValidator.validate(person, result); you have person . You should validate the same object which is preceding BindingResult => Person p or you have to create binding result for particular object explicitly like this

BindingResult errors = new BeanPropertyBindingResult(person,"person");
validate(Person person, Errors errors);

Need Your Help

ASP.NET Gridview Binding to LINQ Dataset ColumnCount

asp.net vb.net linq gridview ado.net

I am trying to bind a asp.net gridview to a linq query result. It works but I am unable to get a count of columns in the resultset. Following is the code in the pageLoad event.

How to align LI vertically middle

html css html-lists vertical-alignment

I've HTML UL LI tags arranged as mentioned below: