Unable to display error on JSP in Spring MVC validation

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.


@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");
return new ModelAndView("redirect:pages/success.jsp");

Please help me.

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

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");

Replied on October 04, 2013
Thanks Arvind. It is working.

Write Answer

©2023 concretepage.com | Privacy Policy | Contact Us