Spring MVC Error Messages
Hello, Spring Fellows,
I have a form that is validated by the Spring Validation once submitted. Each field on the form may contain multiple errors messages if validation fails, so error messages are displayed below the field, not next to it. Here's the code snippet.
<tr>
<td><form:input path="name" /></td>
</tr>
<tr>
<td>
<form:errors path="name*" />
</td>
</tr>
Note that there is a star at the end of the path value to indicate that all error messages for the name
must be displayed.
As you can see, the problem is that, if there is no error message, there will be an extra row on the page that looks out of place to the user. The code above is an overly simplied version, so the actual code has a lot more stuff in it, which prevents me from moving the <form:errors>
tag inside the tag containing the field.
Is there a way to find out if there is any message associated to a given path on the JSP level? Basically, I would like to do the following:
<c:if test="${what do I write here?}">
<tr>
<td>
<form:errors path="name*" />
</td>
</tr>
</c:if>
Thanks!
The 404 error code is configured properly, but it will caused the “. htm” extension handling conflict between the “servlet container” and Spring's “DispatcherServlet“. To solve it, try change the 404. htm to other file extension, for example 404.
Interface BindingResult. General interface that represents binding results. Extends the interface for error registration capabilities, allowing for a Validator to be applied, and adds binding-specific analysis and model building. Serves as result holder for a DataBinder , obtained via the DataBinder.
You can do something like this (notice that bind
is from spring
taglib):
<spring:bind path = "name*">
<c:if test="${status.error}">
<tr>
<td>
<form:errors path="name*" />
</td>
</tr>
</c:if>
</spring:bind>
I solved your problem by doing this:
<table>
<form:errors path="firstName">
<tr>
<td colspan="2">
<form:errors path="firstName"/>
</td>
</tr>
</form:errors>
<tr>
<td><form:label path="firstName"><spring:message code="helloworld.label.firstName"/></form:label></td>
<td><form:input path="firstName"/></td>
</tr>
</table>
errors tag body will be evaluated only if there are errors on the path.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With