Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I've faced the same problem, so I created custom errors tag that displays the first error only - feel free to use it:</p> <p>a] Create custom tag class</p> <pre><code>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 &lt; errorMessages.length; i++) { String errorMessage = errorMessages[i]; if (i &gt; 0) { tagWriter.appendValue(delimiter); } tagWriter.appendValue(getDisplayString(errorMessage)); if (firstErrorOnly) break; } tagWriter.endTag(); } </code></pre> <p>b] To use the custom tag you have to create tag library descriptor - you can simply copy ErrorsTag declaration from spring's tag library descriptors (spring-webmvc-3.2.1.RELEASE.jar in META-INF directory under name <code>spring-form.tld</code>) and add your own attribute <code>firstErrorOnly</code>. Below is the complete example extracted from my library - see comments in the code what can be changed and customized: </p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;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"&gt; &lt;description&gt;Custom extension to Spring Framework JSP Tag Library&lt;/description&gt; &lt;tlib-version&gt;3.0&lt;/tlib-version&gt; &lt;short-name&gt;tags&lt;/short-name&gt; &lt;!-- use your own uri --&gt; &lt;uri&gt;http://cz.devmint.spring-ext/tags&lt;/uri&gt; &lt;tag&gt; &lt;description&gt;Renders field errors in an HTML 'span' tag.&lt;/description&gt; &lt;name&gt;errors&lt;/name&gt; &lt;!-- use your own package - fully qualified name of your tag class --&gt; &lt;tag-class&gt;cz.devmint.springext.web.tags.form.ErrorsTagExt&lt;/tag-class&gt; &lt;body-content&gt;JSP&lt;/body-content&gt; &lt;variable&gt; &lt;name-given&gt;messages&lt;/name-given&gt; &lt;variable-class&gt;java.util.List&lt;/variable-class&gt; &lt;/variable&gt; &lt;!-- this attribute declaration is the only change when compare with spring's original tag definition --&gt; &lt;attribute&gt; &lt;description&gt;Whether to render the first error for given field only&lt;/description&gt; &lt;name&gt;firstErrorOnly&lt;/name&gt; &lt;required&gt;false&lt;/required&gt; &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt; &lt;/attribute&gt; &lt;attribute&gt; &lt;description&gt;Path to errors object for data binding&lt;/description&gt; &lt;name&gt;path&lt;/name&gt; &lt;required&gt;false&lt;/required&gt; &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt; &lt;/attribute&gt; &lt;attribute&gt; &lt;description&gt;HTML Standard Attribute&lt;/description&gt; &lt;name&gt;id&lt;/name&gt; &lt;required&gt;false&lt;/required&gt; &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt; &lt;/attribute&gt; &lt;attribute&gt; &lt;description&gt;Enable/disable HTML escaping of rendered values.&lt;/description&gt; &lt;name&gt;htmlEscape&lt;/name&gt; &lt;required&gt;false&lt;/required&gt; &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt; &lt;/attribute&gt; &lt;attribute&gt; &lt;description&gt;Delimiter for displaying multiple error messages. Defaults to the br tag.&lt;/description&gt; &lt;name&gt;delimiter&lt;/name&gt; &lt;required&gt;false&lt;/required&gt; &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt; &lt;/attribute&gt; &lt;attribute&gt; &lt;description&gt;Equivalent to "class" - HTML Optional Attribute&lt;/description&gt; &lt;name&gt;cssClass&lt;/name&gt; &lt;required&gt;false&lt;/required&gt; &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt; &lt;/attribute&gt; &lt;attribute&gt; &lt;description&gt;Equivalent to "style" - HTML Optional Attribute&lt;/description&gt; &lt;name&gt;cssStyle&lt;/name&gt; &lt;required&gt;false&lt;/required&gt; &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt; &lt;/attribute&gt; &lt;attribute&gt; &lt;description&gt;HTML Standard Attribute&lt;/description&gt; &lt;name&gt;lang&lt;/name&gt; &lt;required&gt;false&lt;/required&gt; &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt; &lt;/attribute&gt; &lt;attribute&gt; &lt;description&gt;HTML Standard Attribute&lt;/description&gt; &lt;name&gt;title&lt;/name&gt; &lt;required&gt;false&lt;/required&gt; &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt; &lt;/attribute&gt; &lt;attribute&gt; &lt;description&gt;HTML Standard Attribute&lt;/description&gt; &lt;name&gt;dir&lt;/name&gt; &lt;required&gt;false&lt;/required&gt; &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt; &lt;/attribute&gt; &lt;attribute&gt; &lt;description&gt;HTML Standard Attribute&lt;/description&gt; &lt;name&gt;tabindex&lt;/name&gt; &lt;required&gt;false&lt;/required&gt; &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt; &lt;/attribute&gt; &lt;attribute&gt; &lt;description&gt;HTML Event Attribute&lt;/description&gt; &lt;name&gt;onclick&lt;/name&gt; &lt;required&gt;false&lt;/required&gt; &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt; &lt;/attribute&gt; &lt;attribute&gt; &lt;description&gt;HTML Event Attribute&lt;/description&gt; &lt;name&gt;ondblclick&lt;/name&gt; &lt;required&gt;false&lt;/required&gt; &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt; &lt;/attribute&gt; &lt;attribute&gt; &lt;description&gt;HTML Event Attribute&lt;/description&gt; &lt;name&gt;onmousedown&lt;/name&gt; &lt;required&gt;false&lt;/required&gt; &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt; &lt;/attribute&gt; &lt;attribute&gt; &lt;description&gt;HTML Event Attribute&lt;/description&gt; &lt;name&gt;onmouseup&lt;/name&gt; &lt;required&gt;false&lt;/required&gt; &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt; &lt;/attribute&gt; &lt;attribute&gt; &lt;description&gt;HTML Event Attribute&lt;/description&gt; &lt;name&gt;onmouseover&lt;/name&gt; &lt;required&gt;false&lt;/required&gt; &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt; &lt;/attribute&gt; &lt;attribute&gt; &lt;description&gt;HTML Event Attribute&lt;/description&gt; &lt;name&gt;onmousemove&lt;/name&gt; &lt;required&gt;false&lt;/required&gt; &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt; &lt;/attribute&gt; &lt;attribute&gt; &lt;description&gt;HTML Event Attribute&lt;/description&gt; &lt;name&gt;onmouseout&lt;/name&gt; &lt;required&gt;false&lt;/required&gt; &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt; &lt;/attribute&gt; &lt;attribute&gt; &lt;description&gt;HTML Event Attribute&lt;/description&gt; &lt;name&gt;onkeypress&lt;/name&gt; &lt;required&gt;false&lt;/required&gt; &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt; &lt;/attribute&gt; &lt;attribute&gt; &lt;description&gt;HTML Event Attribute&lt;/description&gt; &lt;name&gt;onkeyup&lt;/name&gt; &lt;required&gt;false&lt;/required&gt; &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt; &lt;/attribute&gt; &lt;attribute&gt; &lt;description&gt;HTML Event Attribute&lt;/description&gt; &lt;name&gt;onkeydown&lt;/name&gt; &lt;required&gt;false&lt;/required&gt; &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt; &lt;/attribute&gt; &lt;attribute&gt; &lt;description&gt;Specifies the HTML element that is used to render the enclosing errors.&lt;/description&gt; &lt;name&gt;element&lt;/name&gt; &lt;required&gt;false&lt;/required&gt; &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt; &lt;/attribute&gt; &lt;dynamic-attributes&gt;true&lt;/dynamic-attributes&gt; &lt;/tag&gt; &lt;/taglib&gt; </code></pre> <p>Put this xml file in <code>WEB-INF/tld/spring-ext.tld</code></p> <p>On jsp page add declaration:</p> <pre><code>&lt;%@taglib prefix="spring-ext" uri="http://cz.devmint.spring-ext/tags" %&gt; </code></pre> <p>Instead of spring's ErrorsTag use custom tag:</p> <pre><code>&lt;spring-ext:errors path="dummy" firstErrorOnly="true" /&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload