开发者

Spring MVC Error Messages

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!


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜