Spring MVC Error Messages

Spring MVC Error Messages

Hello Spring Fellows,

I have a form that is validated using Spring Validation after submitting. Each field of the form may contain several error messages if verification is not performed, therefore error messages are displayed below the field and not next to it. Here is a snippet of code.

<tr> <td><form:input path="name" /></td> </tr> <tr> <td> <form:errors path="name*" /> </td> </tr> 

Note that there is an asterisk at the end of the path value indicating that all error messages for name should be displayed.

As you can see, the problem is that if there is no error message, the page will have an extra line that looks out of place for the user. The above code is a too simplified version, so there is a lot more material in the code itself, which prevents me from moving the <form:errors> tag inside the tag containing this field.

Is there a way to find out if there is any message associated with this path at 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!

+6
java spring spring-mvc
source share
3 answers

You can do something like this (note that bind is in spring taglib):

 <spring:bind path = "name*"> <c:if test="${status.error}"> <tr> <td> <form:errors path="name*" /> </td> </tr> </c:if> </spring:bind> 
+6
source share

I solved your problem by following these steps:

 <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> 
Error tag body

will only be evaluated if there are errors in the path.

+3
source share

The simplest answer is not to use tables for page layout. Using div tags completely eliminates this problem, since divs are completely dimensionless if set to hidden.

0
source share

All Articles