Home  >  Forum  >  Spring
Post New Query

Unable to display error on JSP in Spring MVC validation


joined on
October 02,2013
Asked on October 04,2013
Hi All,

I am creating application to validate form in Spring MVC. I am not sure why I am not getting error in JSP. Find my controller.

Controller

@RequestMapping(value="user", method = RequestMethod.GET)
public User user(){
return new User(); 
}
@RequestMapping(value="createUser", method = RequestMethod.POST)
public ModelAndView createUser(@ModelAttribute("user") @Valid User user,BindingResult result,ModelMap model) {
   if(result.hasErrors()) {
    return new ModelAndView("redirect:user");
   }
model.addAttribute("name",user.getName());
model.addAttribute("age",user.getAge());
model.addAttribute("location",user.getLocation());
return new ModelAndView("redirect:pages/success.jsp");
}

Please help me.
joined on
October 02,2013
Replied on October 04,2013

JSP is

&ltform:form action="createUser" method="post" commandName='user'&gt
  Enter User Name:&ltform:input  path="name"/&gt
  &ltfont color="red"&gt &ltform:errors path="name"&gt&lt/form:errors&gt&lt/font&gt&ltbr/&gt
  Enter age :&ltform:input path="age"/&gt
  &ltfont color="red"&gt&ltform:errors path="age"&gt&lt/form:errors&gt&lt/font&gt&ltbr/&gt
  Enter location :&ltform:input path="location"/&gt
  &ltfont color="red"&gt&ltform:errors path="location" &gt&lt/form:errors&gt&lt/font&gt&ltbr/&gt
  &ltinput type="submit"&gt
  &lt/form:form&gt
joined on
March 14,2013
Replied on October 04,2013

Using redirect will reset all the errors from BindingResult. You need not to use redirect.
Try as

 if(result.hasErrors()) {
    return new ModelAndView("user");
 }
joined on
October 02,2013
Replied on October 04,2013

Thanks Arvind. It is working.

Write Answer








Copyright ©2017 concretepage.com, all rights reserved |Privacy Policy | Contact Us