简体   繁体   中英

Spring 3 MVC Validation with Hibernate, error after hasErrors?

I am just learning Spring3 Validation with Hibernate. I wanted to add Validation so I pyt the @Valid in the function call and add the code into the formbean but I get the following error if the formbean has a error. if it does not have a error it works great. please et me know what I am NOT doing right. thanks

Source:

@RequestMapping(value = "/save", method = RequestMethod.POST)
public ModelAndView saveArticle(@Valid @ModelAttribute(" article") Article  article, BindingResult result) 
{
System.out.println("In ModelAndView");
// Adding code to check for errors;
if (result.hasErrors())
{
   System.out.println("In ModelAndView-hasErrors");
   return new ModelAndView("addArticle");
}
articleService.addArticle( article);
return new ModelAndView("redirect:/articles.html");
}

Console output:

In ModelAndView
In ModelAndView-hasErrors
Mar 25, 2011 9:41:30 AM org.apache.catalina.core.ApplicationDispatcher invoke
SEVERE: Servlet.service() for servlet jsp threw exception
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'article' available as request attribute

Remove the extra whitespace: @ModelAttribute(" article")

Your model attribute is bound as " article" , whereas your form tries to redisplay a model attribute named "article" , which is obviously not bound.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM