001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.tags.html;
020:
021: import org.apache.beehive.netui.util.internal.InternalStringBuilder;
022:
023: import org.apache.beehive.netui.util.Bundle;
024: import org.apache.beehive.netui.pageflow.internal.InternalUtils;
025: import org.apache.struts.Globals;
026: import org.apache.struts.action.ActionMessage;
027: import org.apache.struts.action.ActionMessages;
028: import org.apache.struts.util.RequestUtils;
029:
030: import javax.servlet.jsp.JspException;
031: import javax.servlet.jsp.PageContext;
032: import java.util.Iterator;
033:
034: /**
035: * Renders an error message with a given error key if that key can be found in the
036: * ActionMessages registered in the <code>pageContext</code> at
037: * <code>org.apache.struts.action.Action.ERROR_KEY</code>. Error ignores its body content.
038: * <p>
039: * The following optional message keys will be utilized if corresponding
040: * messages exist for them in the application resources:
041: * <ul>
042: * <li><b>error.prefix</b> - If present, the corresponding message will be
043: * rendered before each individual error message.</li>
044: * <li><b>error.suffix</b> - If present, the corresponding message will be
045: * rendered after each individual error message.</li>
046: * </ul>
047: * @jsptagref.tagdescription Renders an error message with a given error key if that key can be found in the
048: * ActionMessages registered in the <code>PageContext</code> at
049: * <code>org.apache.struts.action.Action.ERROR_KEY</code>.
050: *
051: * <p>The following optional message keys will be utilized if corresponding
052: * messages exist for them in the application resources:
053: * <blockquote>
054: * <ul>
055: * <li><b>error.prefix</b> - If present, the corresponding message will be
056: * rendered before each individual error message.</li>
057: * <li><b>error.suffix</b> - If present, the corresponding message will be
058: * rendered after each individual error message.</li>
059: * </ul>
060: * </blockquote>
061: * @example In this sample, the "InvalidName" message from the errorMessages bundle will be used to output the error.
062: * <pre>
063: * <netui:error bundleName="errorMessages" key="InvalidName"/></pre>
064: * @netui:tag name="error" body-content="empty" description="Renders an error message with a given error key."
065: */
066: public class Error extends ErrorBaseTag {
067: private String _key = null; // error key name, may be an expression
068:
069: /**
070: * Return the name of the Tag.
071: */
072: public String getTagName() {
073: return "Error";
074: }
075:
076: /**
077: * Set the key under which the error was stored (often the name of the form bean property associated with the error).
078: * @param key the key under which the error was stored
079: * @jsptagref.attributedescription The key under which the error was stored (often the name of the form bean property associated with the error)
080: * @jsptagref.databindable false
081: * @jsptagref.attributesyntaxvalue <i>string_or_expression_key</i>
082: * @netui:attribute required="true" rtexprvalue="true"
083: * description="The key under which the error was stored (often the name of the form bean property associated with the error)"
084: */
085: public void setKey(String key) throws JspException {
086: _key = setRequiredValueAttribute(key, "key");
087: }
088:
089: /**
090: * Render the specified error message if it can be found.
091: * @throws JspException if a JSP exception has occurred
092: */
093: public void doTag() throws JspException {
094: // Error will try an always work even if there are expression
095: // errors. The error will be reported at the end.
096:
097: PageContext pageContext = getPageContext();
098:
099: // Were any error messages specified?
100: ActionMessages errors = null;
101: try {
102: errors = RequestUtils.getActionMessages(pageContext,
103: Globals.ERROR_KEY);
104: } catch (JspException e) {
105: RequestUtils.saveException(pageContext, e);
106: throw e;
107: }
108:
109: // report any expression errors
110: if ((errors == null) || errors.isEmpty()) {
111: if (hasErrors())
112: reportErrors();
113: return;
114: }
115:
116: String qualifiedBundle = InternalUtils.getQualifiedBundleName(
117: _bundleName, pageContext.getRequest());
118:
119: boolean prefixPresent = false;
120: boolean suffixPresent = false;
121: String locale = _locale;
122:
123: if (!isMissingUserDefaultMessages(pageContext)) {
124: try {
125: // Check for presence of error prefix and suffix message keys
126: prefixPresent = RequestUtils.present(pageContext,
127: qualifiedBundle, locale, "error.prefix");
128: suffixPresent = RequestUtils.present(pageContext,
129: qualifiedBundle, locale, "error.suffix");
130: } catch (JspException e) {
131: String s = Bundle.getString("Tags_ErrorsException",
132: new Object[] { e.getMessage() });
133: registerTagError(s, null);
134: reportErrors();
135: return;
136: }
137: }
138:
139: // Render the error message appropriately
140: InternalStringBuilder results = new InternalStringBuilder(128);
141:
142: String message = null;
143: Iterator reports = null;
144: reports = errors.get(_key);
145:
146: while (reports.hasNext()) {
147: ActionMessage report = (ActionMessage) reports.next();
148: if (prefixPresent) {
149: message = RequestUtils.message(pageContext,
150: qualifiedBundle, locale, "error.prefix");
151: results.append(message);
152: }
153:
154: message = getErrorMessage(report, qualifiedBundle);
155:
156: if (message != null) {
157: results.append(message);
158: results.append("\r\n");
159: }
160: if (suffixPresent) {
161:
162: message = RequestUtils.message(pageContext,
163: qualifiedBundle, locale, "error.suffix");
164: results.append(message);
165: }
166: }
167:
168: write(results.toString());
169:
170: // report any expression errors
171: if (hasErrors())
172: reportErrors();
173: }
174: }
|