【问题标题】:Handling multiple validation annotations error messages处理多个验证注释错误消息
【发布时间】:2013-02-10 06:34:08
【问题描述】:

我正在使用注释在 SpringMVC 中尝试验证...

我对一个字段使用了 2 个注释来验证

@NotEmpty(message="required")

@Size(min="3" max="8" message="超出范围")

私人字符串密码;

我面临的问题是,当该字段留空时,它会显示两个错误消息(*必需以及超出范围)。但我想显示其中一个错误消息,而不是两者都显示......

是否可以通过一条消息进行限制?如果是这样,这种情况的可能性是什么?

任何建议和指导表示赞赏..提前感谢...

【问题讨论】:

    标签: spring validation spring-mvc hibernate-annotations spring-annotations


    【解决方案1】:

    有一个关于那个的问题。您可以查看以下链接:

    In Spring MVC validation, Is it possible to show only one error message per field at a time?

    【讨论】:

    • 感谢您的回答 ahmetdursun... 我看到了您发布的链接,在这种情况下,他们使用了接口和自己的验证,因此他们使用了“@ReportAsSingleViolation”,但在我的情况下,我使用的是预定义的异常注释在我无法放置“@ReportAsSingleViolation”注释的课堂上......还有其他方法吗?
    【解决方案2】:

    我也遇到了同样的问题,所以我创建了只显示第一个错误的自定义错误标签 - 随意使用它:

    a] 创建自定义标签类

    package cz.devmint.springext.web.tags.form;
    
    import javax.servlet.jsp.JspException;
    
    import org.apache.commons.lang.StringUtils;
    import org.springframework.util.ObjectUtils;
    import org.springframework.web.servlet.tags.form.ErrorsTag;
    import org.springframework.web.servlet.tags.form.TagWriter;
    
    public class ErrorsTagExt extends ErrorsTag {
    
    private boolean firstErrorOnly = true;
    
    public boolean isFirstErrorOnly() {
        return firstErrorOnly;
    }
    
    public void setFirstErrorOnly(boolean firstErrorOnly) {
        this.firstErrorOnly = firstErrorOnly;
    }
    
    @Override
    protected void renderDefaultContent(TagWriter tagWriter) throws JspException {
        tagWriter.startTag(getElement());
        writeDefaultAttributes(tagWriter);
        String delimiter = ObjectUtils.getDisplayString(evaluate("delimiter", getDelimiter()));
        String[] errorMessages = getBindStatus().getErrorMessages();
        for(int i = 0; i < errorMessages.length; i++) {
            String errorMessage = errorMessages[i];
            if (i > 0) {
                tagWriter.appendValue(delimiter);
            }
            tagWriter.appendValue(getDisplayString(errorMessage));
            if (firstErrorOnly) break;
        }
        tagWriter.endTag();
    }
    

    b] 要使用自定义标签,您必须创建标签库描述符 - 您可以简单地从 spring 的标签库描述符中复制 ErrorsTag 声明(在 META-INF 目录中名为 @987654322 的 spring-webmvc-3.2.1.RELEASE.jar @) 并添加您自己的属性firstErrorOnly。下面是从我的库中提取的完整示例 - 请参阅代码中的 cmets 可以更改和自定义的内容:

    <?xml version="1.0" encoding="UTF-8"?>
    <taglib xmlns="http://java.sun.com/xml/ns/j2ee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
        version="2.0">
    
    <description>Custom extension to Spring Framework JSP Tag Library</description>
    <tlib-version>3.0</tlib-version>
    <short-name>tags</short-name>
    <!-- use your own uri -->
    <uri>http://cz.devmint.spring-ext/tags</uri>
    <tag>
        <description>Renders field errors in an HTML 'span' tag.</description>
        <name>errors</name>
        <!-- use your own package - fully qualified name of your tag class  -->
        <tag-class>cz.devmint.springext.web.tags.form.ErrorsTagExt</tag-class>
        <body-content>JSP</body-content>
        <variable>
            <name-given>messages</name-given>
            <variable-class>java.util.List</variable-class>
        </variable>
        <!-- this attribute declaration is the only change when compare with spring's original tag definition -->   
        <attribute>
            <description>Whether to render the first error for given field only</description>
            <name>firstErrorOnly</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <description>Path to errors object for data binding</description>
            <name>path</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <description>HTML Standard Attribute</description>
            <name>id</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <description>Enable/disable HTML escaping of rendered values.</description>
            <name>htmlEscape</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <description>Delimiter for displaying multiple error messages. Defaults to the br tag.</description>
            <name>delimiter</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <description>Equivalent to "class" - HTML Optional Attribute</description>
            <name>cssClass</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <description>Equivalent to "style" - HTML Optional Attribute</description>
            <name>cssStyle</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <description>HTML Standard Attribute</description>
            <name>lang</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <description>HTML Standard Attribute</description>
            <name>title</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <description>HTML Standard Attribute</description>
            <name>dir</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <description>HTML Standard Attribute</description>
            <name>tabindex</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <description>HTML Event Attribute</description>
            <name>onclick</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <description>HTML Event Attribute</description>
            <name>ondblclick</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <description>HTML Event Attribute</description>
            <name>onmousedown</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <description>HTML Event Attribute</description>
            <name>onmouseup</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <description>HTML Event Attribute</description>
            <name>onmouseover</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <description>HTML Event Attribute</description>
            <name>onmousemove</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <description>HTML Event Attribute</description>
            <name>onmouseout</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <description>HTML Event Attribute</description>
            <name>onkeypress</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <description>HTML Event Attribute</description>
            <name>onkeyup</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <description>HTML Event Attribute</description>
            <name>onkeydown</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <description>Specifies the HTML element that is used to render the enclosing errors.</description>
            <name>element</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <dynamic-attributes>true</dynamic-attributes>
    </tag>
    </taglib>
    

    将此xml文件放入WEB-INF/tld/spring-ext.tld

    在jsp页面添加声明:

    <%@taglib prefix="spring-ext" uri="http://cz.devmint.spring-ext/tags" %>    
    

    使用自定义标签代替spring的ErrorsTag:

    <spring-ext:errors path="dummy" firstErrorOnly="true" /> 
    

    【讨论】:

    • 您好,我可以理解您提供的代码,但很抱歉,我不知道在哪里使用此代码以及如何将其与错误标签进行映射。我真的很抱歉我是 Spring 新手...请您解释一下如何使用此代码...
    • 好的,我已经添加了完整的代码和特定步骤的解释。
    • 非常感谢你让我的一天快乐,你做得很好,它的工作方式与我预期的一样。再次感谢你的知识分享......
    • 真的很抱歉 user2003170,我无法为您的答案投票。我没有多少声望可以投票...
    • user2003170 如果不定义GroupSequence,如何保证属性验证的顺序?如果我们不这样做,那么每次属性验证的顺序都是随机的。
    猜你喜欢
    • 2017-09-28
    • 1970-01-01
    • 2018-06-08
    • 2011-10-21
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 2019-04-28
    • 1970-01-01
    相关资源
    最近更新 更多