Spring MVC <form:errors/> tag doesn't find error messages

此生再无相见时 提交于 2020-01-12 23:03:36

问题


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?


回答1:


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.




回答2:


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




回答3:


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.




回答4:


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.




回答5:


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.




回答6:


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.



来源:https://stackoverflow.com/questions/1363753/spring-mvc-formerrors-tag-doesnt-find-error-messages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!