KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > input > Hidden


1 /*
2  * Copyright 1999,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 package org.apache.taglibs.input;
17
18 import java.util.Map JavaDoc;
19
20 import javax.servlet.ServletRequest JavaDoc;
21 import javax.servlet.jsp.JspException JavaDoc;
22 import javax.servlet.jsp.JspTagException JavaDoc;
23 import javax.servlet.jsp.JspWriter JavaDoc;
24 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
25
26 /**
27  *
28  * This class implements the <input:hidden> tag, which presents an
29  * <input type="hidden" ... /> form element.
30  *
31  * @version 0.90
32  * @author Shawn Bayern
33  * @author Lance Lavandowska
34  * @author Karl von Randow
35  */

36
37 public class Hidden extends TagSupport JavaDoc {
38
39     private String JavaDoc name; // name of the hidden field
40

41     private String JavaDoc dVal; // default value if none is found
42

43     private Map JavaDoc attributes; // attributes of the <input> element
44

45     private String JavaDoc attributesText; // attributes of the <input> element as text
46

47     private String JavaDoc beanId; // bean id to get default values from
48

49     public void release() {
50         super.release();
51         name = null;
52         dVal = null;
53         attributes = null;
54         attributesText = null;
55         beanId = null;
56     }
57
58     public int doStartTag() throws JspException JavaDoc {
59         try {
60             // sanity check
61
if (name == null || name.equals(""))
62                 throw new JspTagException JavaDoc("invalid null or empty 'name'");
63
64             // Store beanId in a local variable as we change it
65
String JavaDoc beanId = this.beanId;
66
67             // Get default beanId
68
if (beanId == null) {
69                 beanId = Util.defaultFormBeanId(this);
70             } else if (beanId.length() == 0) {
71                 // An empty beanId means, do not use any bean - not even default
72
beanId = null;
73             }
74
75             // get what we need from the page
76
ServletRequest JavaDoc req = pageContext.getRequest();
77             JspWriter JavaDoc out = pageContext.getOut();
78
79             // start building up the tag
80
out.print("<input type=\"hidden\" ");
81             out.print("name=\"" + Util.quote(name) + "\" ");
82
83             // include any attributes we've got here
84
Util.printAttributes(out, attributes);
85             if (attributesText != null) {
86                 out.print(attributesText + " ");
87             }
88
89             /*
90              * print out the value from the bean if it's there, or from the
91              * request if it's there, or use the default value if it's not
92              */

93             String JavaDoc beanValue = (beanId != null ? Util.beanPropertyValue(
94                     pageContext.findAttribute(beanId), name) : null);
95             if (beanValue != null) {
96                 out.print("value=\"" + Util.quote(beanValue) + "\" ");
97             } else if (req.getParameter(name) != null) {
98                 out.print("value=\"" + Util.quote(req.getParameter(name))
99                         + "\" ");
100             } else {
101                 if (dVal != null)
102                     out.print("value=\"" + Util.quote(dVal) + "\" ");
103                 else
104                     out.print("value=\"\" ");
105             }
106             // end the tag
107
out.print("/>");
108
109         } catch (Exception JavaDoc ex) {
110             throw new JspTagException JavaDoc(ex.getMessage());
111         }
112         return SKIP_BODY;
113     }
114
115     public void setName(String JavaDoc x) {
116         name = x;
117     }
118
119     public void setAttributes(Map JavaDoc x) {
120         attributes = x;
121     }
122
123     public void setAttributesText(String JavaDoc x) {
124         attributesText = x;
125     }
126
127     public void setBean(String JavaDoc x) {
128         beanId = x;
129     }
130
131     public void setDefault(String JavaDoc x) {
132         dVal = x;
133     }
134
135     /**
136      * Getter for property name.
137      *
138      * @return Value of property name.
139      */

140     public String JavaDoc getName() {
141         return name;
142     }
143
144     /**
145      * Getter for property default.
146      *
147      * @return Value of property default.
148      */

149     public String JavaDoc getDefault() {
150         return dVal;
151     }
152
153     /**
154      * Getter for property bean.
155      *
156      * @return Value of property bean.
157      */

158     public String JavaDoc getBean() {
159         return beanId;
160     }
161
162     /**
163      * Getter for property attributesText.
164      *
165      * @return Value of property attributesText.
166      */

167     public String JavaDoc getAttributesText() {
168         return attributesText;
169     }
170
171     /**
172      * Getter for property attributes.
173      *
174      * @return Value of property attributes.
175      */

176     public Map JavaDoc getAttributes() {
177         return attributes;
178     }
179 }
Popular Tags