1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.struts.taglib.html;
19
20 import org.apache.struts.Globals;
21 import org.apache.struts.action.ActionMessage;
22 import org.apache.struts.action.ActionMessages;
23 import org.apache.struts.taglib.TagUtils;
24 import org.apache.struts.util.MessageResources;
25
26 import javax.servlet.jsp.JspException;
27 import javax.servlet.jsp.tagext.TagSupport;
28
29 import java.util.Iterator;
30
31 /***
32 * Custom tag that renders error messages if an appropriate request attribute
33 * has been created. The tag looks for a request attribute with a reserved
34 * key, and assumes that it is either a String, a String array, containing
35 * message keys to be looked up in the module's MessageResources, or an object
36 * of type <code>org.apache.struts.action.ActionErrors</code>. <p> The
37 * following optional message keys will be utilized if corresponding messages
38 * exist for them in the application resources:
39 *
40 * <ul>
41 *
42 * <li><b>errors.header</b> - If present, the corresponding message will be
43 * rendered prior to the individual list of error messages.</li>
44 *
45 * <li><b>errors.footer</b> - If present, the corresponding message will be
46 * rendered following the individual list of error messages.</li>
47 *
48 * <li><b>errors.prefix</b> - If present, the corresponding message will be
49 * rendered before each individual error message.</li>
50 *
51 * <li><b>errors.suffix</b> - If present, the corresponding message will be
52 * rendered after each individual error message.</li>
53 *
54 * </ul>
55 *
56 * @version $Rev: 376841 $ $Date: 2005-08-21 19:08:45 -0400 (Sun, 21 Aug 2005)
57 * $
58 */
59 public class ErrorsTag extends TagSupport {
60 /***
61 * The message resources for this package.
62 */
63 protected static MessageResources messages =
64 MessageResources.getMessageResources(Constants.Package
65 + ".LocalStrings");
66
67
68
69 /***
70 * The servlet context attribute key for our resources.
71 */
72 protected String bundle = null;
73
74 /***
75 * The session attribute key for our locale.
76 */
77 protected String locale = Globals.LOCALE_KEY;
78
79 /***
80 * The request attribute key for our error messages (if any).
81 */
82 protected String name = Globals.ERROR_KEY;
83
84 /***
85 * The name of the property for which error messages should be returned,
86 * or <code>null</code> to return all errors.
87 */
88 protected String property = null;
89
90 /***
91 * The message resource key for errors header.
92 */
93 protected String header = null;
94
95 /***
96 * The message resource key for errors footer.
97 */
98 protected String footer = null;
99
100 /***
101 * The message resource key for errors prefix.
102 */
103 protected String prefix = null;
104
105 /***
106 * The message resource key for errors suffix.
107 */
108 protected String suffix = null;
109
110 public String getBundle() {
111 return (this.bundle);
112 }
113
114 public void setBundle(String bundle) {
115 this.bundle = bundle;
116 }
117
118 public String getLocale() {
119 return (this.locale);
120 }
121
122 public void setLocale(String locale) {
123 this.locale = locale;
124 }
125
126 public String getName() {
127 return (this.name);
128 }
129
130 public void setName(String name) {
131 this.name = name;
132 }
133
134 public String getProperty() {
135 return (this.property);
136 }
137
138 public void setProperty(String property) {
139 this.property = property;
140 }
141
142 public String getHeader() {
143 return (header == null) ? "errors.header" : header;
144 }
145
146 public void setHeader(String header) {
147 this.header = header;
148 }
149
150 public String getFooter() {
151 return (footer == null) ? "errors.footer" : footer;
152 }
153
154 public void setFooter(String footer) {
155 this.footer = footer;
156 }
157
158 public String getPrefix() {
159 return (prefix == null) ? "errors.prefix" : prefix;
160 }
161
162 public void setPrefix(String prefix) {
163 this.prefix = prefix;
164 }
165
166 public String getSuffix() {
167 return (suffix == null) ? "errors.suffix" : suffix;
168 }
169
170 public void setSuffix(String suffix) {
171 this.suffix = suffix;
172 }
173
174
175
176 /***
177 * Render the specified error messages if there are any.
178 *
179 * @throws JspException if a JSP exception has occurred
180 */
181 public int doStartTag() throws JspException {
182
183 ActionMessages errors = null;
184
185 try {
186 errors =
187 TagUtils.getInstance().getActionMessages(pageContext, name);
188 } catch (JspException e) {
189 TagUtils.getInstance().saveException(pageContext, e);
190 throw e;
191 }
192
193 if ((errors == null) || errors.isEmpty()) {
194 return (EVAL_BODY_INCLUDE);
195 }
196
197 boolean headerPresent =
198 TagUtils.getInstance().present(pageContext, bundle, locale,
199 getHeader());
200
201 boolean footerPresent =
202 TagUtils.getInstance().present(pageContext, bundle, locale,
203 getFooter());
204
205 boolean prefixPresent =
206 TagUtils.getInstance().present(pageContext, bundle, locale,
207 getPrefix());
208
209 boolean suffixPresent =
210 TagUtils.getInstance().present(pageContext, bundle, locale,
211 getSuffix());
212
213
214 StringBuffer results = new StringBuffer();
215 boolean headerDone = false;
216 String message = null;
217 Iterator reports =
218 (property == null) ? errors.get() : errors.get(property);
219
220 while (reports.hasNext()) {
221 ActionMessage report = (ActionMessage) reports.next();
222
223 if (!headerDone) {
224 if (headerPresent) {
225 message =
226 TagUtils.getInstance().message(pageContext, bundle,
227 locale, getHeader());
228
229 results.append(message);
230 }
231
232 headerDone = true;
233 }
234
235 if (prefixPresent) {
236 message =
237 TagUtils.getInstance().message(pageContext, bundle, locale,
238 getPrefix());
239 results.append(message);
240 }
241
242 if (report.isResource()) {
243 message =
244 TagUtils.getInstance().message(pageContext, bundle, locale,
245 report.getKey(), report.getValues());
246 } else {
247 message = report.getKey();
248 }
249
250 if (message != null) {
251 results.append(message);
252 }
253
254 if (suffixPresent) {
255 message =
256 TagUtils.getInstance().message(pageContext, bundle, locale,
257 getSuffix());
258 results.append(message);
259 }
260 }
261
262 if (headerDone && footerPresent) {
263 message =
264 TagUtils.getInstance().message(pageContext, bundle, locale,
265 getFooter());
266 results.append(message);
267 }
268
269 TagUtils.getInstance().write(pageContext, results.toString());
270
271 return (EVAL_BODY_INCLUDE);
272 }
273
274 /***
275 * Release any acquired resources.
276 */
277 public void release() {
278 super.release();
279 bundle = Globals.MESSAGES_KEY;
280 locale = Globals.LOCALE_KEY;
281 name = Globals.ERROR_KEY;
282 property = null;
283 header = null;
284 footer = null;
285 prefix = null;
286 suffix = null;
287 }
288 }