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