Spring 3 MVC 使用 Hibernate 进行验证,hasErrors 之后出现错误?

发布于 2024-10-27 00:25:58 字数 988 浏览 2 评论 0原文

我正在学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜心小果奶 2024-11-03 00:25:58

删除多余的空格:@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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文