Spring MVC错误消息 [英] Spring MVC Error Messages

查看:149
本文介绍了Spring MVC错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Spring MVC错误消息

Spring MVC Error Messages

Hello,Spring Fellows,

Hello, Spring Fellows,

我有一个表格经过验证Spring验证一旦提交。如果验证失败,表单上的每个字段可能包含多个错误消息,因此错误消息显示在字段下方,而不是旁边。这是代码段。

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>

请注意,路径值末尾有一个星号表示所有错误消息必须显示 name

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.

如您所见,问题在于,如果没有错误消息,页面上会有一个额外的行,对用户来说不合适。上面的代码是一个过于简单的版本,所以实际的代码中有更多的东西,这阻止我移动标签内的< form:errors> 标签包含该字段。

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.

有没有办法找出JSP级别上是否有与给定路径关联的消息?基本上,我想做以下事情:

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>

谢谢!

推荐答案

你可以这样做(注意 bind 来自 spring taglib):

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>

这篇关于Spring MVC错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆