Spring MVC <form:errors/> tag doesn't fi

2019-04-07 15:48发布

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?

6条回答
The star\"
2楼-- · 2019-04-07 15:51

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.

查看更多
时光不老,我们不散
3楼-- · 2019-04-07 15:52

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

查看更多
孤傲高冷的网名
4楼-- · 2019-04-07 15:55

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楼-- · 2019-04-07 16:03

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楼-- · 2019-04-07 16:04

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.

查看更多
戒情不戒烟
7楼-- · 2019-04-07 16:17

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.

查看更多
登录 后发表回答