Spring 3 MVC 使用 Hibernate 进行验证,hasErrors 之后出现错误?
我正在学习 Spring3 与 Hibernate 的验证。我想添加验证,因此我在函数调用中 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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
删除多余的空格:
@ModelAttribute("article")
您的模型属性绑定为
"article"
,而您的表单尝试重新显示名为"的模型属性文章”
,显然没有绑定。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.