同じ問題に直面したので、最初のエラーのみを表示するカスタムエラータグを作成しました。自由に使用してください。
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のタグライブラリ記述子(名前の下のMETA-INFディレクトリにあるspring-webmvc-3.2.1.RELEASE.jar)からErrorsTag宣言をコピーして、独自のタグをspring-form.tld
追加するだけです。属性 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" />