当前位置: 首页 > 知识库问答 >
问题:

处理多个验证注释错误消息

孔琪
2023-03-14

我正在使用注释在SpringMVC中进行验证。。。

我对一个字段使用了两个注释进行验证

@NotEmpty(message=“required”)

@大小(最小值为3,最大值为8,消息为超出范围)

私有字符串密码;

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

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

任何建议和指导都很感激...提前感谢...

共有2个答案

叶经略
2023-03-14

这是一个问题。您可以查看以下链接:

在Spring MVC验证中,每个字段一次只能显示一条错误消息吗?

秦彦君
2023-03-14

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

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的标记库描述符(namespring form.tld下META-INF目录中的spring-webmvc-3.2.1.RELEASE.jar)中复制ErrorsTag声明,并添加自己的属性firstErrorOnly。以下是从我的库中提取的完整示例-请参阅代码中的注释,可以更改和自定义哪些内容:

<?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" /> 
 类似资料:
  • 在添加com.google.dagger:dagger-android-support依赖项后,我得到了这个gradle错误。 这是我的构建的一部分

  • 问题内容: 我是新手,发现错误处理非常冗长。我已经读过它的理由并大体上同意,但是似乎在某些地方似乎有更多代码来处理错误而不是实际工作。这是一个(人为的)示例,我在其中传送“ Hello world!”。进入cat并读取并打印输出。基本上,每一行都可以再处理三个错误,而我什至没有处理任何事情。 有没有惯用的,干净的方法来处理此问题?我只是觉得我在想什么。 问题答案: 显然,我们必须处理任何错误。我们

  • 我有一个具有类型为的字段的实体,在该字段上我定义了一个注释,强制执行与给定正则表达式匹配的字段内容。现在我想知道,是否可以参数化约束,例如在定义的验证错误消息中显示与正则表达式不匹配的第一个字符。 更糟糕的是,要显示的约束违反错误消息不是直接在注释中定义的,而是在属性文件中定义的,如下面所示的示例: 示例类: 示例属性文件: 有可能用javax做这样的事情吗。验证?我假设注释无法参数化错误消息。但

  • 问题内容: 我刚刚开始测试JSR-303 Bean验证,并且想知道是否有可能。我们的应用程序中有一个用于所有String字段的默认正则表达式。如果我想使用bean验证来应用此功能,我想我需要在表单对象中注释每个字段。 一击是否可以将@Pattern应用于所有字符串(或某些字段)?我们正在WebLogic 10.3.4上使用Hibernate实现,并以JSF2.0为前端。验证应该独立于视图,因为我可

  • 在多模块Android项目中,注释处理仍然是在任何编译完成之前的第一个任务,然后触发完整的编译。当然,这是在进入< code>app模块之前按模块完成的。 想象一下这样一种情况,一些子模块依赖于其他子模块,编译将会失败,因为依赖者还不能在它所依赖的模块中找到生成类的定义,仅仅因为它们还没有生成。 我想知道是否有一种方法可以使用<code>gradle</code>或任何其他方法强制执行所有子模块注

  • 我试图让非空注释在NetBeans中工作,这是一个又一个令人头痛的问题。 我遵循了https://checkerframework.org/manual/#netbeans的说明,可以确认是作为编译库和处理器库添加的。我启用了注释处理,并添加了作为注释处理器。 在源代码中,我可以导入import和have注释编译。然而,在构建过程中,我发现: 我需要做什么来解决此错误?