001: /*
002: * $Id: ErrorsTag.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts.taglib.html;
022:
023: import org.apache.struts.Globals;
024: import org.apache.struts.action.ActionMessage;
025: import org.apache.struts.action.ActionMessages;
026: import org.apache.struts.taglib.TagUtils;
027: import org.apache.struts.util.MessageResources;
028:
029: import javax.servlet.jsp.JspException;
030: import javax.servlet.jsp.tagext.TagSupport;
031:
032: import java.util.Iterator;
033:
034: /**
035: * Custom tag that renders error messages if an appropriate request attribute
036: * has been created. The tag looks for a request attribute with a reserved
037: * key, and assumes that it is either a String, a String array, containing
038: * message keys to be looked up in the module's MessageResources, or an object
039: * of type <code>org.apache.struts.action.ActionErrors</code>. <p> The
040: * following optional message keys will be utilized if corresponding messages
041: * exist for them in the application resources:
042: *
043: * <ul>
044: *
045: * <li><b>errors.header</b> - If present, the corresponding message will be
046: * rendered prior to the individual list of error messages.</li>
047: *
048: * <li><b>errors.footer</b> - If present, the corresponding message will be
049: * rendered following the individual list of error messages.</li>
050: *
051: * <li><b>errors.prefix</b> - If present, the corresponding message will be
052: * rendered before each individual error message.</li>
053: *
054: * <li><b>errors.suffix</b> - If present, the corresponding message will be
055: * rendered after each individual error message.</li>
056: *
057: * </ul>
058: *
059: * @version $Rev: 471754 $ $Date: 2005-08-21 19:08:45 -0400 (Sun, 21 Aug 2005)
060: * $
061: */
062: public class ErrorsTag extends TagSupport {
063: /**
064: * The message resources for this package.
065: */
066: protected static MessageResources messages = MessageResources
067: .getMessageResources(Constants.Package + ".LocalStrings");
068:
069: // ----------------------------------------------------------- Properties
070:
071: /**
072: * The servlet context attribute key for our resources.
073: */
074: protected String bundle = null;
075:
076: /**
077: * The session attribute key for our locale.
078: */
079: protected String locale = Globals.LOCALE_KEY;
080:
081: /**
082: * The request attribute key for our error messages (if any).
083: */
084: protected String name = Globals.ERROR_KEY;
085:
086: /**
087: * The name of the property for which error messages should be returned,
088: * or <code>null</code> to return all errors.
089: */
090: protected String property = null;
091:
092: /**
093: * The message resource key for errors header.
094: */
095: protected String header = null;
096:
097: /**
098: * The message resource key for errors footer.
099: */
100: protected String footer = null;
101:
102: /**
103: * The message resource key for errors prefix.
104: */
105: protected String prefix = null;
106:
107: /**
108: * The message resource key for errors suffix.
109: */
110: protected String suffix = null;
111:
112: public String getBundle() {
113: return (this .bundle);
114: }
115:
116: public void setBundle(String bundle) {
117: this .bundle = bundle;
118: }
119:
120: public String getLocale() {
121: return (this .locale);
122: }
123:
124: public void setLocale(String locale) {
125: this .locale = locale;
126: }
127:
128: public String getName() {
129: return (this .name);
130: }
131:
132: public void setName(String name) {
133: this .name = name;
134: }
135:
136: public String getProperty() {
137: return (this .property);
138: }
139:
140: public void setProperty(String property) {
141: this .property = property;
142: }
143:
144: public String getHeader() {
145: return (header == null) ? "errors.header" : header;
146: }
147:
148: public void setHeader(String header) {
149: this .header = header;
150: }
151:
152: public String getFooter() {
153: return (footer == null) ? "errors.footer" : footer;
154: }
155:
156: public void setFooter(String footer) {
157: this .footer = footer;
158: }
159:
160: public String getPrefix() {
161: return (prefix == null) ? "errors.prefix" : prefix;
162: }
163:
164: public void setPrefix(String prefix) {
165: this .prefix = prefix;
166: }
167:
168: public String getSuffix() {
169: return (suffix == null) ? "errors.suffix" : suffix;
170: }
171:
172: public void setSuffix(String suffix) {
173: this .suffix = suffix;
174: }
175:
176: // ------------------------------------------------------- Public Methods
177:
178: /**
179: * Render the specified error messages if there are any.
180: *
181: * @throws JspException if a JSP exception has occurred
182: */
183: public int doStartTag() throws JspException {
184: // Were any error messages specified?
185: ActionMessages errors = null;
186:
187: try {
188: errors = TagUtils.getInstance().getActionMessages(
189: pageContext, name);
190: } catch (JspException e) {
191: TagUtils.getInstance().saveException(pageContext, e);
192: throw e;
193: }
194:
195: if ((errors == null) || errors.isEmpty()) {
196: return (EVAL_BODY_INCLUDE);
197: }
198:
199: boolean headerPresent = TagUtils.getInstance().present(
200: pageContext, bundle, locale, getHeader());
201:
202: boolean footerPresent = TagUtils.getInstance().present(
203: pageContext, bundle, locale, getFooter());
204:
205: boolean prefixPresent = TagUtils.getInstance().present(
206: pageContext, bundle, locale, getPrefix());
207:
208: boolean suffixPresent = TagUtils.getInstance().present(
209: pageContext, bundle, locale, getSuffix());
210:
211: // Render the error messages appropriately
212: StringBuffer results = new StringBuffer();
213: boolean headerDone = false;
214: String message = null;
215: Iterator reports = (property == null) ? errors.get() : errors
216: .get(property);
217:
218: while (reports.hasNext()) {
219: ActionMessage report = (ActionMessage) reports.next();
220:
221: if (!headerDone) {
222: if (headerPresent) {
223: message = TagUtils.getInstance().message(
224: pageContext, bundle, locale, getHeader());
225:
226: results.append(message);
227: }
228:
229: headerDone = true;
230: }
231:
232: if (prefixPresent) {
233: message = TagUtils.getInstance().message(pageContext,
234: bundle, locale, getPrefix());
235: results.append(message);
236: }
237:
238: if (report.isResource()) {
239: message = TagUtils.getInstance().message(pageContext,
240: bundle, locale, report.getKey(),
241: report.getValues());
242: } else {
243: message = report.getKey();
244: }
245:
246: if (message != null) {
247: results.append(message);
248: }
249:
250: if (suffixPresent) {
251: message = TagUtils.getInstance().message(pageContext,
252: bundle, locale, getSuffix());
253: results.append(message);
254: }
255: }
256:
257: if (headerDone && footerPresent) {
258: message = TagUtils.getInstance().message(pageContext,
259: bundle, locale, getFooter());
260: results.append(message);
261: }
262:
263: TagUtils.getInstance().write(pageContext, results.toString());
264:
265: return (EVAL_BODY_INCLUDE);
266: }
267:
268: /**
269: * Release any acquired resources.
270: */
271: public void release() {
272: super.release();
273: bundle = Globals.MESSAGES_KEY;
274: locale = Globals.LOCALE_KEY;
275: name = Globals.ERROR_KEY;
276: property = null;
277: header = null;
278: footer = null;
279: prefix = null;
280: suffix = null;
281: }
282: }
|