KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tags > databinding > pageinput > DeclarePageInput


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.pageinput;
19
20 import java.util.Map JavaDoc;
21 import javax.servlet.jsp.JspException JavaDoc;
22 import javax.servlet.jsp.PageContext JavaDoc;
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24
25 import org.apache.beehive.netui.pageflow.internal.InternalUtils;
26 import org.apache.beehive.netui.util.Bundle;
27 import org.apache.beehive.netui.util.logging.Logger;
28 import org.apache.beehive.netui.tags.AbstractSimpleTag;
29
30 /**
31  * <p>
32  * The DeclarePageInput tag is used to declare variables that are passed as outputs of Page Flow actions to a
33  * JSP. This allows pages to declare a data contract that invoking actions must satisfy in order to
34  * successfully render a page; in essence, this is a simple method signature for the JSP.
35  * </p>
36  * <p>
37  * Page inputs are added to a Page Flow's {@link org.apache.beehive.netui.pageflow.Forward} class via the
38  * {@link org.apache.beehive.netui.pageflow.Forward#addActionOutput(String, Object)} method. From the page's
39  * perspective, the action outputs are known as <i>page inputs</i> and are available via the JSP EL
40  * implicit object <code>pageInput</code> using the name given them on the
41  * {@link org.apache.beehive.netui.pageflow.Forward} and set on this tag via {@link #setName(String)}.
42  * </p>
43  * <p>
44  * A page input can be declared to be required; if required, the page input must be available in
45  * the map of action outputs passed to the page.
46  * </p>
47  * <p>
48  * For example, to add an actiout output called <code>profile</code> to a {@link org.apache.beehive.netui.pageflow.Forward}
49  * an action would contain code like:<br/>
50  * <pre>
51  * forward.addActionOutput("profile", yourProfile);
52  * </pre>
53  * In order to declare this as a page input in a JSP, the page would contain a tag as:
54  * <pre>
55  * &lt;netui-data:declarePageInput name="profile" type="org.someprofile.ProfileBean"/>
56  * </pre>
57  * and the <code>profile</code> object could be referenced in the JSP as:
58  * <pre>
59  * ${pageInput.profile}
60  * </pre>
61  * </p>
62  *
63  * @jsptagref.tagdescription
64  * <p>
65  * The DeclarePageInput tag is used to declare variables that are passed as outputs of Page Flow actions to a
66  * JSP. This allows pages to declare a data contract that invoking actions must satisfy in order to
67  * successfully render a page; in essence, this is a simple method signature for the JSP.
68  * </p>
69  * <p>
70  * Page inputs are added to a Page Flow's {@link org.apache.beehive.netui.pageflow.Forward} class via the
71  * {@link org.apache.beehive.netui.pageflow.Forward#addActionOutput(String, Object)} method. From the page's
72  * perspective, the action outputs are known as <i>page inputs</i> and are available via the JSP EL
73  * implicit object <code>pageInput</code> using the name given them on the
74  * {@link org.apache.beehive.netui.pageflow.Forward} and set on this tag via {@link #setName(String)}.
75  * </p>
76  * <p>
77  * A page input can be declared to be required; if required, the page input must be available in
78  * the map of action outputs passed to the page.
79  * </p>
80  * <p>
81  * For example, to add an actiout output called <code>profile</code> to a {@link org.apache.beehive.netui.pageflow.Forward}
82  * an action would contain code like:<br/>
83  * <pre>
84  * forward.addActionOutput("profile", yourProfile);
85  * </pre>
86  * In order to declare this as a page input in a JSP, the page would contain a tag as:
87  * <pre>
88  * &lt;netui-data:declarePageInput name="profile" type="org.someprofile.ProfileBean"/>
89  * </pre>
90  * and the <code>profile</code> object could be referenced in the JSP as:
91  * <pre>
92  * ${pageInput.profile}
93  * </pre>
94  * </p>
95  *
96  * @netui:tag name="declarePageInput" body-content="scriptless"
97  * description="Use this tag to declare a page input variable that is available in the pageInput databinding context."
98  */

99 public class DeclarePageInput
100     extends AbstractSimpleTag {
101
102     private static final Logger LOGGER = Logger.getInstance(DeclarePageInput.class);
103     private static final String JavaDoc EMPTY_STRING = "";
104
105     private String JavaDoc _type = null;
106     private String JavaDoc _name = null;
107     private boolean _required = true;
108
109     /**
110      * Get the name of this tag. This is used to identify the type of this tag
111      * for reporting tag errors.
112      *
113      * @return a constant String representing the name of this tag.
114      */

115     public String JavaDoc getTagName() {
116         return "DeclarePageInput";
117     }
118
119     /**
120      * Set the name of a variable that can be referecned using the page
121      * input data binding context.
122      *
123      * @param name the name of the variable
124      * @jsptagref.attributedescription The name of the variable to reference.
125      * @jsptagref.attributesyntaxvalue <i>string_name</i>
126      * @netui:attribute required="true"
127      */

128     public void setName(String JavaDoc name) {
129         _name = name;
130     }
131
132     /**
133      * Set a flag that declares whether this page intput is required or optiona. If a Page Input is required,
134      * the tag will report an error if the page input key does not appear in the set of page inputs for a page.
135      *
136      * @param required whether to require the page input for the page
137      * @jsptagref.attributedescription
138      * Set a flag that declares whether this page intput is required or optiona. If a Page Input is required,
139      * the tag will report an error if the page input key does not appear in the set of page inputs for a page.
140      * @jsptagref.attributesyntaxvalue <i>string_name</i>
141      * @netui:attribute required="false"
142      */

143     public void setRequired(boolean required) {
144         _required = required;
145     }
146
147     /**
148      * Set the String classname variable that represents the expected type of the page input.
149      *
150      * @param type the type of the variable that is referenced
151      * @jsptagref.attributedescription
152      * Set the String classname variable that represents the expected type of the page input.
153      * @jsptagref.attributesyntaxvalue <i>string_type</i>
154      * @netui:attribute required="true"
155      */

156     public void setType(String JavaDoc type) {
157         _type = type;
158     }
159
160     /**
161      *
162      */

163     public void doTag()
164         throws JspException JavaDoc {
165
166         LOGGER.debug("Added a page input named \"" + _name + "\" of type \"" + _type + "\"");
167
168         if(_name.equals(EMPTY_STRING)) {
169             String JavaDoc msg = Bundle.getErrorString("Tags_DeclarePageInput_EmptyName");
170             registerTagError(msg, null);
171         }
172
173         if(_type.equals(EMPTY_STRING)) {
174             String JavaDoc msg = Bundle.getErrorString("Tags_DeclarePageInput_EmptyType");
175             registerTagError(msg, null);
176         }
177
178         if(_required) {
179             assert getJspContext() instanceof PageContext JavaDoc;
180             assert ((PageContext JavaDoc)getJspContext()).getRequest() instanceof HttpServletRequest JavaDoc;
181
182             HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc)((PageContext JavaDoc)getJspContext()).getRequest();
183             Map JavaDoc actionOutputMap = InternalUtils.getPageInputMap(request);
184             if(actionOutputMap == null || !actionOutputMap.containsKey(_name)) {
185                 String JavaDoc msg = Bundle.getErrorString("Tags_DeclarePageInput_Required", new Object JavaDoc[]{_name});
186                 registerTagError(msg, null);
187             }
188         }
189
190         if(hasErrors())
191             reportErrors();
192     }
193 }
194
Popular Tags