Spring MVC<形式 :错误>>标签未找到错误消息

发布于 2024-08-03 08:45:17 字数 265 浏览 3 评论 0原文

我与一位编写 JSP 文件的前端开发人员一起工作。我们有一个工作正常的表单,除了验证/绑定/处理错误似乎无法用 Spring 的 标记显示。

我已经确认正在设置错误,并且显然错误的正确路径是什么。据说 应该将它们全部呈现,无论路径如何,但它什么也不显示。

我是否需要进入标签库源代码来推断出了什么问题?

I work with a front-end developer who writes JSP files. We have a form that is working correctly, except validation/binding/processing errors can't seem to be displayed with Spring's <form:errors/> tag.

I've confirmed that the error is being set, and what is apparently the correct path for the errors. Supposedly <form:errors path="*" /> should render them all, regardless of path, but it shows nothing.

Do I need to get into the tag library source to deduce what's going wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

甜嗑 2024-08-10 08:45:17

我发现了两件事。

1) 确保在表单标记中指定 form-b​​ean / 命令对象的名称

<form:form method="post" enctype="multipart/form-data" commandName="salesOrder">

2) 确保按类名命名 form-b​​ean / 命令对象。在上面的示例中,我的类是 com.abc.xyz.SalesOrder。如果我在模型中将其称为“so”或“order”,那么它不会显示错误。

2 things I discovered.

1) make sure you specify the name of the form-bean / command object in the form tag

<form:form method="post" enctype="multipart/form-data" commandName="salesOrder">

2) make sure you name your form-bean / command object by its class name. In the example above my class is com.abc.xyz.SalesOrder. If I call it "so" or "order" in the model then it will not show the errors.

愿与i 2024-08-10 08:45:17

简单回答:>必须位于内元素以便绑定到模型的“命令”对象。

Simple answer: <form:errors/> must be within a <form:form/> element in order to bind to the model's "command" object.

小嗷兮 2024-08-10 08:45:17

问题 - 为什么“form:error path="xyzProperty”不在 jsp 上打印错误?

答案 -

  1. BindingResult 确实具有 objectName 属性,该属性将错误列表与 jsp 中的 commandName 绑定。

  2. 默认 objectName = 您的对象名称。例如,如果类名称是 MyCareerFB,则 objectName = myCareerFB。请注意第一个字符是小写的,它遵循 bean 命名约定。

  3. 保持 jsp 中的 commandName 值与 objectName 相同,否则不会绑定错误。 object和jsp永远不会打印错误消息。

Question - Why "form:error path="xyzProperty" doesn't print error on jsp?

Anserwer -

  1. BindingResult does have objectName property which binds list of errors with commandName in your jsp.

  2. Defualt objectName = your Object Name. eg if class name is MyCareerFB then objectName = myCareerFB. Mind the first character in small case, it follows bean nameing convention.

  3. Keep commandName value in jsp same as objectName, else error wont be binded with object and jsp will never print error message.

诺曦 2024-08-10 08:45:17

这只是为了后代的缘故,因为答案已经被接受了。我自己也有同样的症状,但对我来说问题是 form:form 方法属性值区分大小写:即 method="post" 不会显示错误,而 method="POST" 则可以正常工作。这里特别值得注意的是,一切都按预期进行——表单视图按预期显示,因为验证失败,除了错误在最终的 JSP 中不可见。

此行为将存在于扩展 AbstractFormController 的任何控制器上,因为

protected boolean isFormSubmission(HttpServletRequest request)

它是“POST”.equals 而不是“POST”.equalsIgnoreCase。

This is just for posterity's sake, seeing that an answer has already been accepted. I had the same symptoms myself, but the problem for me was that the form:form method attribute value is case-sensitive: i.e. method="post" will not show errors, while method="POST" will work fine. Of particular note here is that everything worked as expected -- The form view was displayed as expected since validation had failed EXCEPT that the errors were not visible in the final JSP.

This behavior will exist on any controller that extends AbstractFormController, since

protected boolean isFormSubmission(HttpServletRequest request)

does a "POST".equals instead of "POST".equalsIgnoreCase.

够运 2024-08-10 08:45:17

不知道我是否也遇到同样的问题。我的问题是我为 @ModelAttribute 设置了错误的值。将值设置为的 commandName工作正常。

Don't know if I had the same problem. My problem was that I set the wrong value for @ModelAttribute. Having the value set to the commandName of the <form:form /> works fine.

云朵有点甜 2024-08-10 08:45:17

您可能没有对 commandName 表单属性使用正确的命名约定。这就是我遇到的问题。我有一个名为“XYZTask”的类,并将该表单命名为commandName =“xyztask”。除了我没有看到标签报告的错误之外,所有表单映射都有效。我将我的类重命名为“XyzTask”和形式 commandName="xyzTask",并且错误开始出现。

You might have not used the right naming convention for the commandName form attribute. That was the problem I ran into. I had a class named "XYZTask" and I named the form commandName="xyztask". All the form mapping worked except I did not see the errors reported by the tag. I renamed my class to "XyzTask" and the form commandName="xyzTask", and the errors started working.

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