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.databinding.script;
020:
021: import javax.servlet.jsp.JspException;
022:
023: import org.apache.beehive.netui.tags.AbstractClassicTag;
024: import org.apache.beehive.netui.util.logging.Logger;
025:
026: /**
027: * <p>
028: * This tag evaluates an expression and places the result in the
029: * {@link javax.servlet.jsp.PageContext} object, where the data is available to the JSP EL and JSP scriptlet.
030: * This tag can be used to extract data from forms, Controller files, and any data binding context and
031: * make it available to scriptlets.
032: * </p>
033: * <p>
034: * In the following example, the getData tag gets the value of a property in the page flow and
035: * makes it available to the JSP via the JSP EL implicit object <code>${pageScope}</code>.
036: * <pre>
037: * <netui-data:getData resultId="myData" value="${pageFlow.myData}"/>
038: * </pre>
039: * <p>
040: * The following scriptlet extracts the data from the <code>PageContext</code> object and writes it to
041: * the rendered HTML: <br/>
042: * <pre>
043: * ${pageScope.myData}
044: * </pre>
045: * </p>
046: * In this first example, the <netui-data:getData> tag loads data into the {@link javax.servlet.jsp.PageContext}'s
047: * attribute map. It can then be accessed using the {@link javax.servlet.jsp.PageContext#getAttribute(String)} method.
048: * <pre>
049: * <netui:form action="lastNameAction" focus="lastname">
050: * ...
051: * <netui-data:getData resultId="first" value="${actionForm.firstname}"/>
052: * ...
053: * <%
054: * String firstName = (String)pageContext.getAttribute("first");
055: * System.out.println("First Name = " + firstName);
056: * ...
057: * %>
058: * ...
059: * </netui:form></pre>
060: * <p/>
061: * <p>
062: * This example shows how to use <netui-data:getData> and the <code>PageContext</code> inside of other
063: * containers, in this case a <netui-data:repeater> tag. The <netui-data:getData> below extracts each
064: * element as the <netui-data:repeater> iterates over the data set and writes it to the Java console:
065: * <br/>
066: * <pre> <netui-data:repeater dataSource="pageFlow.strArr">
067: * ...
068: * <netui-data:repeaterItem>
069: * <netui:span value="${container.item}" />
070: * <netui-data:getData resultId="item" value="${container.item}"/>
071: * <%
072: * String currentItem = (String) pageContext.getAttribute("item");
073: * System.out.println(currentItem);
074: * ...
075: * %>
076: * </netui-data:repeaterItem>
077: * ...
078: * </netui-data:repeater>
079: * </pre>
080: * </p>
081: *
082: * @jsptagref.tagdescription
083: * <p>
084: * This tag evaluates an expression and places the result in the
085: * {@link javax.servlet.jsp.PageContext} object, where the data is available to the JSP EL and JSP scriptlet.
086: * This tag can be used to extract data from forms, Controller files, and any data binding context and
087: * make it available to scriptlets.
088: * </p>
089: * <p>
090: * In the following example, the getData tag gets the value of a property in the page flow and
091: * makes it available to the JSP via the JSP EL implicit object <code>${pageScope}</code>.
092: * <pre>
093: * <netui-data:getData resultId="myData" value="${pageFlow.myData}"/>
094: * </pre>
095: * <p>
096: * The following scriptlet extracts the data from the <code>PageContext</code> object and writes it to
097: * the rendered HTML: <br/>
098: * <pre>
099: * ${pageScope.myData}
100: * </pre>
101: *
102: * @example
103: * In this first example, the <netui-data:getData> tag loads data into the {@link javax.servlet.jsp.PageContext}'s
104: * attribute map. It can then be accessed using the {@link javax.servlet.jsp.PageContext#getAttribute(String)} method.
105: * <pre>
106: * <netui:form action="lastNameAction" focus="lastname">
107: * ...
108: * <netui-data:getData resultId="first" value="${actionForm.firstname}"/>
109: * ...
110: * <%
111: * String firstName = (String)pageContext.getAttribute("first");
112: * System.out.println("First Name = " + firstName);
113: * ...
114: * %>
115: * ...
116: * </netui:form></pre>
117: * <p/>
118: * <p>
119: * This example shows how to use <netui-data:getData> and the <code>PageContext</code> inside of other
120: * containers, in this case a <netui-data:repeater> tag. The <netui-data:getData> below extracts each
121: * element as the <netui-data:repeater> iterates over the data set and writes it to the Java console:
122: * <br/>
123: * <pre> <netui-data:repeater dataSource="pageFlow.strArr">
124: * ...
125: * <netui-data:repeaterItem>
126: * <netui:span value="${container.item}" />
127: * <netui-data:getData resultId="item" value="${container.item}"/>
128: * <%
129: * String currentItem = (String) pageContext.getAttribute("item");
130: * System.out.println(currentItem);
131: * ...
132: * %>
133: * </netui-data:repeaterItem>
134: * ...
135: * </netui-data:repeater>
136: * </pre>
137: * </p>
138: * @netui:tag name="getData"
139: * description="Evaluates an expression and places the result in the JSP's PageContext. Can be used to extract objects from forms, page flows, and other objects that can be databound. You can then write a scriplet to access the data by using the getAttribute method of javax.servlet.jsp.PageContext."
140: * @deprecated The JSP expression language should be used instead.
141: */
142: public class GetData extends AbstractClassicTag {
143:
144: private static final Logger LOGGER = Logger
145: .getInstance(GetData.class);
146:
147: private Object _value = null;
148: private String _resultId = null;
149:
150: /**
151: * Get the name of this tag. This is used to identify the type of this tag
152: * for reporting tag errors.
153: *
154: * @return a constant String representing the name of this tag.
155: */
156: public String getTagName() {
157: return "GetData";
158: }
159:
160: /**
161: * The data binding expression to evaluate. The result will be stored in the {@link javax.servlet.jsp.PageContext}
162: * object as specified in the <code>resultId</code> attribute.
163: *
164: * @param value the expression to evaluate
165: * @jsptagref.attributedescription
166: * The data binding expression to evaluate. The result will be stored in the {@link javax.servlet.jsp.PageContext}
167: * object as specified in the <code>resultId</code> attribute.
168: * @jsptagref.attributesyntaxvalue <i>expression_value</i>
169: * @netui:attribute required="true" rtexprvalue="true"
170: */
171: public void setValue(Object value) {
172: _value = value;
173: }
174:
175: /**
176: * Set the String attribute name under which the result of evaluating an expression will be stored in
177: * the {@link javax.servlet.jsp.PageContext}..
178: *
179: * @param resultId the String key
180: * @jsptagref.attributedescription
181: * Specifies the property of the PageContext object where the data will be stored.
182: * @jsptagref.attributesyntaxvalue <i>string_resultId</i>
183: * @netui:attribute required="true"
184: */
185: public void setResultId(String resultId) {
186: _resultId = resultId;
187: }
188:
189: /**
190: * Start the tag evaluation. This tag ignores its body content.
191: *
192: * @return {@link #SKIP_BODY}
193: */
194: public int doStartTag() {
195: return SKIP_BODY;
196: }
197:
198: /**
199: * Evaluate the expression at the <code>value</code> attribute and store the result in the
200: * {@link javax.servlet.jsp.PageContext} under the attribute key specified in {@link #setResultId(String)}.
201: * If an existing key in the PageContext's attribute map exists, a warning will be written to the log file.
202: * If errors occur during expression evaluation, they will be reported in the JSP page. If the value
203: * returned by the expression is null, an attribute named <code>resultId</code> will be removed from
204: * the PageContext's attribute map.
205: *
206: * @return {@link #EVAL_PAGE}
207: */
208: public int doEndTag() throws JspException {
209: if (_value != null) {
210: if (LOGGER.isInfoEnabled()
211: && pageContext.getAttribute(_resultId) != null)
212: LOGGER
213: .info("Overwriting a value in PageContext attribute map with key \""
214: + _resultId
215: + "\" and object of type \""
216: + _value.getClass().getName());
217:
218: pageContext.setAttribute(_resultId, _value);
219: } else {
220: if (LOGGER.isInfoEnabled())
221: LOGGER
222: .info("Removing a value from the PageContext attribute map with key \""
223: + _resultId
224: + "\". The object returned by the expression is null.");
225:
226: pageContext.removeAttribute(_resultId);
227: }
228:
229: localRelease();
230: return EVAL_PAGE;
231: }
232:
233: /**
234: * Reset all of the fields of this tag.
235: */
236: protected void localRelease() {
237: super.localRelease();
238: _resultId = null;
239: _value = null;
240: }
241: }
|