我刚学习使用Hibernate进行Spring3验证。我想添加验证,所以我在函数调用中pyt @Valid并将代码添加到formbean中,但如果formbean有错误,我会收到以下错误。如果它没有错误,它的效果很好。请知道我做得对不对。感谢
来源:
@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");
}
控制台输出:
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
答案 0 :(得分:0)
删除额外的空格:@ModelAttribute(" article")
您的模型属性绑定为" article"
,而您的表单尝试重新显示名为"article"
的模型属性,这显然不受约束。