KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tags > databinding > script > GetData


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18 package org.apache.beehive.netui.tags.databinding.script;
19
20 import javax.servlet.jsp.JspException JavaDoc;
21
22 import org.apache.beehive.netui.tags.AbstractClassicTag;
23 import org.apache.beehive.netui.util.logging.Logger;
24
25 /**
26  * <p>
27  * This tag evaluates an expression and places the result in the
28  * {@link javax.servlet.jsp.PageContext} object, where the data is available to the JSP EL and JSP scriptlet.
29  * This tag can be used to extract data from forms, Controller files, and any data binding context and
30  * make it available to scriptlets.
31  * </p>
32  * <p>
33  * In the following example, the getData tag gets the value of a property in the page flow and
34  * makes it available to the JSP via the JSP EL implicit object <code>${pageScope}</code>.
35  * <pre>
36  * &lt;netui-data:getData resultId="myData" value="${pageFlow.myData}"/>
37  * </pre>
38  * <p>
39  * The following scriptlet extracts the data from the <code>PageContext</code> object and writes it to
40  * the rendered HTML: <br/>
41  * <pre>
42  * ${pageScope.myData}
43  * </pre>
44  * </p>
45  * In this first example, the &lt;netui-data:getData> tag loads data into the {@link javax.servlet.jsp.PageContext}'s
46  * attribute map. It can then be accessed using the {@link javax.servlet.jsp.PageContext#getAttribute(String)} method.
47  * <pre>
48  * &lt;netui:form action="lastNameAction" focus="lastname"&gt;
49  * ...
50  * &lt;netui-data:getData resultId="first" value="${actionForm.firstname}"/&gt;
51  * ...
52  * &lt;%
53  * String firstName = (String)pageContext.getAttribute("first");
54  * System.out.println("First Name = " + firstName);
55  * ...
56  * %&gt;
57  * ...
58  * &lt;/netui:form&gt;</pre>
59  * <p/>
60  * <p>
61  * This example shows how to use &lt;netui-data:getData&gt; and the <code>PageContext</code> inside of other
62  * containers, in this case a &lt;netui-data:repeater> tag. The &lt;netui-data:getData> below extracts each
63  * element as the &lt;netui-data:repeater> iterates over the data set and writes it to the Java console:
64  * <br/>
65  * <pre> &lt;netui-data:repeater dataSource="pageFlow.strArr"&gt;
66  * ...
67  * &lt;netui-data:repeaterItem&gt;
68  * &lt;netui:span value="${container.item}" /&gt;
69  * &lt;netui-data:getData resultId="item" value="${container.item}"/&gt;
70  * &lt;%
71  * String currentItem = (String) pageContext.getAttribute("item");
72  * System.out.println(currentItem);
73  * ...
74  * %&gt;
75  * &lt;/netui-data:repeaterItem&gt;
76  * ...
77  * &lt;/netui-data:repeater&gt;
78  * </pre>
79  * </p>
80  *
81  * @jsptagref.tagdescription
82  * <p>
83  * This tag evaluates an expression and places the result in the
84  * {@link javax.servlet.jsp.PageContext} object, where the data is available to the JSP EL and JSP scriptlet.
85  * This tag can be used to extract data from forms, Controller files, and any data binding context and
86  * make it available to scriptlets.
87  * </p>
88  * <p>
89  * In the following example, the getData tag gets the value of a property in the page flow and
90  * makes it available to the JSP via the JSP EL implicit object <code>${pageScope}</code>.
91  * <pre>
92  * &lt;netui-data:getData resultId="myData" value="${pageFlow.myData}"/>
93  * </pre>
94  * <p>
95  * The following scriptlet extracts the data from the <code>PageContext</code> object and writes it to
96  * the rendered HTML: <br/>
97  * <pre>
98  * ${pageScope.myData}
99  * </pre>
100  *
101  * @example
102  * In this first example, the &lt;netui-data:getData> tag loads data into the {@link javax.servlet.jsp.PageContext}'s
103  * attribute map. It can then be accessed using the {@link javax.servlet.jsp.PageContext#getAttribute(String)} method.
104  * <pre>
105  * &lt;netui:form action="lastNameAction" focus="lastname"&gt;
106  * ...
107  * &lt;netui-data:getData resultId="first" value="${actionForm.firstname}"/&gt;
108  * ...
109  * &lt;%
110  * String firstName = (String)pageContext.getAttribute("first");
111  * System.out.println("First Name = " + firstName);
112  * ...
113  * %&gt;
114  * ...
115  * &lt;/netui:form&gt;</pre>
116  * <p/>
117  * <p>
118  * This example shows how to use &lt;netui-data:getData&gt; and the <code>PageContext</code> inside of other
119  * containers, in this case a &lt;netui-data:repeater> tag. The &lt;netui-data:getData> below extracts each
120  * element as the &lt;netui-data:repeater> iterates over the data set and writes it to the Java console:
121  * <br/>
122  * <pre> &lt;netui-data:repeater dataSource="pageFlow.strArr"&gt;
123  * ...
124  * &lt;netui-data:repeaterItem&gt;
125  * &lt;netui:span value="${container.item}" /&gt;
126  * &lt;netui-data:getData resultId="item" value="${container.item}"/&gt;
127  * &lt;%
128  * String currentItem = (String) pageContext.getAttribute("item");
129  * System.out.println(currentItem);
130  * ...
131  * %&gt;
132  * &lt;/netui-data:repeaterItem&gt;
133  * ...
134  * &lt;/netui-data:repeater&gt;
135  * </pre>
136  * </p>
137  * @netui:tag name="getData"
138  * 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."
139  */

140 public class GetData
141         extends AbstractClassicTag {
142
143     private static final Logger LOGGER = Logger.getInstance(GetData.class);
144
145     private Object JavaDoc _value = null;
146     private String JavaDoc _resultId = null;
147
148     /**
149      * Get the name of this tag. This is used to identify the type of this tag
150      * for reporting tag errors.
151      *
152      * @return a constant String representing the name of this tag.
153      */

154     public String JavaDoc getTagName() {
155         return "GetData";
156     }
157
158     /**
159      * The data binding expression to evaluate. The result will be stored in the {@link javax.servlet.jsp.PageContext}
160      * object as specified in the <code>resultId</code> attribute.
161      *
162      * @param value the expression to evaluate
163      * @jsptagref.attributedescription
164      * The data binding expression to evaluate. The result will be stored in the {@link javax.servlet.jsp.PageContext}
165      * object as specified in the <code>resultId</code> attribute.
166      * @jsptagref.attributesyntaxvalue <i>expression_value</i>
167      * @netui:attribute required="true" rtexprvalue="true"
168      */

169     public void setValue(Object JavaDoc value) {
170         _value = value;
171     }
172
173     /**
174      * Set the String attribute name under which the result of evaluating an expression will be stored in
175      * the {@link javax.servlet.jsp.PageContext}..
176      *
177      * @param resultId the String key
178      * @jsptagref.attributedescription
179      * Specifies the property of the PageContext object where the data will be stored.
180      * @jsptagref.attributesyntaxvalue <i>string_resultId</i>
181      * @netui:attribute required="true"
182      */

183     public void setResultId(String JavaDoc resultId) {
184         _resultId = resultId;
185     }
186
187     /**
188      * Start the tag evaluation. This tag ignores its body content.
189      *
190      * @return {@link #SKIP_BODY}
191      */

192     public int doStartTag() {
193         return SKIP_BODY;
194     }
195
196     /**
197      * Evaluate the expression at the <code>value</code> attribute and store the result in the
198      * {@link javax.servlet.jsp.PageContext} under the attribute key specified in {@link #setResultId(String)}.
199      * If an existing key in the PageContext's attribute map exists, a warning will be written to the log file.
200      * If errors occur during expression evaluation, they will be reported in the JSP page. If the value
201      * returned by the expression is null, an attribute named <code>resultId</code> will be removed from
202      * the PageContext's attribute map.
203      *
204      * @return {@link #EVAL_PAGE}
205      */

206     public int doEndTag()
207             throws JspException JavaDoc {
208         if(_value != null) {
209             if(LOGGER.isInfoEnabled() && pageContext.getAttribute(_resultId) != null)
210                 LOGGER.info("Overwriting a value in PageContext attribute map with key \"" + _resultId +
211                         "\" and object of type \"" + _value.getClass().getName());
212
213             pageContext.setAttribute(_resultId, _value);
214         }
215         else {
216             if(LOGGER.isInfoEnabled())
217                 LOGGER.info("Removing a value from the PageContext attribute map with key \"" + _resultId +
218                         "\". The object returned by the expression is null.");
219
220             pageContext.removeAttribute(_resultId);
221         }
222
223         localRelease();
224         return EVAL_PAGE;
225     }
226
227     /**
228      * Reset all of the fields of this tag.
229      */

230     protected void localRelease() {
231         super.localRelease();
232         _resultId = null;
233         _value = null;
234     }
235 }
236
Popular Tags