Spring MVC tag <form: errors / "> does not find error messages
I work with a third-party developer who writes JSP files. We have a form that works correctly, except that validation / binding / processing errors cannot be displayed with the Spring <form:errors/> .
I have confirmed that the error is fixed, and apparently the correct path for errors. Presumably, <form:errors path="*" /> should display them all, regardless of the path, but show nothing.
Do I need to go to the tag library source to determine what is going wrong?
The simple answer: <form: errors / "> must be inside the <form: form /"> element in order to bind to the model command object.
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 with your class name. In the above example, my class is com.abc.xyz.SalesOrder. If I call it โthisโ or โorderโ in the model, it will not show an error.
Question - Why? form: error path = "xyzProperty" doesn't print error on jsp?
Anserver -
BindingResult has an objectName property that associates a list of errors with the command name in your jsp.
Defualt objectName = the name of your object. for example, if the class name is MyCareerFB, then objectName = myCareerFB. Pay attention to the first character in the small case, this follows the bean symbol.
Store the value of commandName in jsp in the same way as objectName, otherwise the error will not be bound to the object, and jsp will never print an error message.
This is only for posterity, because the answer has already been accepted. I had the same symptoms, but the problem for me was that the value of the form: form method attribute is case sensitive: for example, method = "message" will not show errors, and method = "POST" will work fine. Of particular note is that everything worked as expected. The form view was displayed as expected, because the validation failed. EXCEPT that errors were not visible in the final JSP.
This behavior will exist on any controller that extends AbstractFormController, since
protected boolean isFormSubmission(HttpServletRequest request) executes "POST" .equals instead of "POST" .equalsIgnoreCase.
I donโt know if I had the same problem. My problem was that I set the wrong value for @ModelAttribute. The value specified for the <form: form /> command name works fine.
You may not have used the correct naming convention for the commandName form attribute. That was the problem I encountered. I had a class called "XYZTask" and I named the form commandName = "xyztask". The entire form display worked, except that I did not see errors tagged. I renamed my class to "XyzTask" and to the form commandName = "xyzTask", and the errors started to work.