KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > tags > form > ErrorsTag


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.web.servlet.tags.form;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Arrays JavaDoc;
21 import java.util.List JavaDoc;
22
23 import javax.servlet.jsp.JspException JavaDoc;
24 import javax.servlet.jsp.PageContext JavaDoc;
25 import javax.servlet.jsp.tagext.BodyTag JavaDoc;
26
27 import org.springframework.util.Assert;
28 import org.springframework.util.ObjectUtils;
29
30 /**
31  * Form tag for displaying errors for a particular field or object.
32  *
33  * <p>This tag supports three main usage patterns:
34  *
35  * <ol>
36  * <li>Field only - set '<code>path</code>' to the field name (or path)</li>
37  * <li>Object errors only - omit '<code>path</code>'</li>
38  * <li>All errors - set '<code>path</code>' to '<code>*</code>'</li>
39  * </ol>
40  *
41  * @author Rob Harrop
42  * @author Juergen Hoeller
43  * @author Rick Evans
44  * @since 2.0
45  */

46 public class ErrorsTag extends AbstractHtmlElementBodyTag implements BodyTag JavaDoc {
47
48     /**
49      * The key under which this tag exposes error messages in
50      * the {@link PageContext#PAGE_SCOPE page context scope}.
51      */

52     public static final String JavaDoc MESSAGES_ATTRIBUTE = "messages";
53
54     /**
55      * The HTML '<code>span</code>' tag.
56      */

57     public static final String JavaDoc SPAN_TAG = "span";
58
59
60     private String JavaDoc element = SPAN_TAG;
61
62     private String JavaDoc delimiter = "<br/>";
63
64     /**
65      * Stores any value that existed in the 'errors messages' before the tag was started.
66      */

67     private Object JavaDoc oldMessages;
68
69     private boolean errorMessagesWereExposed;
70
71
72     /**
73      * Set the HTML element must be used to render the error messages.
74      * <p>Defaults to an HTML '<code>&lt;span/&gt;</code>' tag.
75      */

76     public void setElement(String JavaDoc element) {
77         Assert.hasText(element, "'element' cannot be null or blank");
78         this.element = element;
79     }
80
81     /**
82      * Get the HTML element must be used to render the error messages.
83      */

84     public String JavaDoc getElement() {
85         return this.element;
86     }
87
88     /**
89      * Set the delimiter to be used between error messages.
90      * <p>Defaults to an HTML '<code>&lt;br/&gt;</code>' tag.
91      */

92     public void setDelimiter(String JavaDoc delimiter) {
93         this.delimiter = delimiter;
94     }
95
96     /**
97      * Return the delimiter to be used between error messages.
98      */

99     public String JavaDoc getDelimiter() {
100         return this.delimiter;
101     }
102
103
104     /**
105      * Get the value for the HTML '<code>name</code>' attribute.
106      * <p>Simply returns <code>""</code> (the empty string) because the
107      * '<code>name</code>' attribute is not a validate attribute for the
108      * '<code>span</code>' element.
109      */

110     protected String JavaDoc getName() throws JspException JavaDoc {
111         return "";
112     }
113
114     /**
115      * Get the value for the HTML '<code>id</code>' attribute.
116      * <p>Appends '<code>.errors</code>' to the value returned by {@link #getCompletePath()}.
117      * @return the value for the HTML '<code>name</code>' attribute
118      * @see #getPath()
119      */

120     protected String JavaDoc autogenerateId() throws JspException JavaDoc {
121         return getCompletePath() + ".errors";
122     }
123
124     /**
125      * Should rendering of this tag proceed at all?
126      * <p>Only renders output when there are errors for the configured {@link #setPath path}.
127      * @return <code>true</code> only when there are errors for the configured {@link #setPath path}
128      */

129     protected boolean shouldRender() throws JspException JavaDoc {
130         try {
131             return getBindStatus().isError();
132         }
133         catch (IllegalStateException JavaDoc ex) {
134             // Neither BindingResult nor target object available.
135
return false;
136         }
137     }
138
139     protected void renderDefaultContent(TagWriter tagWriter) throws JspException JavaDoc {
140         tagWriter.startTag(getElement());
141         writeDefaultAttributes(tagWriter);
142         String JavaDoc delimiter = ObjectUtils.getDisplayString(evaluate("delimiter", getDelimiter()));
143         String JavaDoc[] errorMessages = getBindStatus().getErrorMessages();
144         for (int i = 0; i < errorMessages.length; i++) {
145             String JavaDoc errorMessage = errorMessages[i];
146             if (i > 0) {
147                 tagWriter.appendValue(delimiter);
148             }
149             tagWriter.appendValue(getDisplayString(errorMessage));
150         }
151         tagWriter.endTag();
152     }
153
154     /**
155      * Exposes any bind status error messages under {@link #MESSAGES_ATTRIBUTE this key}
156      * in the {@link PageContext#PAGE_SCOPE}.
157      * <p>Only called if {@link #shouldRender()} returns <code>true</code>.
158      * @see #removeAttributes()
159      */

160     protected void exposeAttributes() throws JspException JavaDoc {
161         List JavaDoc errorMessages = new ArrayList JavaDoc();
162         errorMessages.addAll(Arrays.asList(getBindStatus().getErrorMessages()));
163         this.oldMessages = this.pageContext.getAttribute(MESSAGES_ATTRIBUTE, PageContext.PAGE_SCOPE);
164         this.pageContext.setAttribute(MESSAGES_ATTRIBUTE, errorMessages, PageContext.PAGE_SCOPE);
165         this.errorMessagesWereExposed = true;
166     }
167
168     /**
169      * Removes any bind status error messages that were previously stored under
170      * {@link #MESSAGES_ATTRIBUTE this key} in the {@link PageContext#PAGE_SCOPE}.
171      * @see #exposeAttributes()
172      */

173     protected void removeAttributes() {
174         if (this.errorMessagesWereExposed) {
175             if (this.oldMessages != null) {
176                 this.pageContext.setAttribute(MESSAGES_ATTRIBUTE, this.oldMessages, PageContext.PAGE_SCOPE);
177                 this.oldMessages = null;
178             }
179             else {
180                 this.pageContext.removeAttribute(MESSAGES_ATTRIBUTE, PageContext.PAGE_SCOPE);
181             }
182         }
183     }
184
185 }
186
Popular Tags