KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > view > jsp > html > FormTag


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.verge.mvc.view.jsp.html;
8
9
10 import java.io.IOException JavaDoc;
11 import java.util.HashMap JavaDoc;
12 import java.util.Map JavaDoc;
13
14 import javax.servlet.jsp.JspException JavaDoc;
15
16 import com.inversoft.verge.mvc.view.HtmlConstants;
17 import com.inversoft.verge.mvc.view.HtmlViewToolkit;
18 import com.inversoft.verge.mvc.view.jsp.JspTools;
19
20
21 /**
22  * This class is the base HTML form tag.
23  *
24  * @author Brian Pontarelli
25  */

26 public class FormTag extends ContextBasedTag {
27
28     private String JavaDoc action;
29     protected String JavaDoc localAction;
30     private String JavaDoc method;
31     protected String JavaDoc localMethod;
32     private String JavaDoc onsubmit;
33     protected String JavaDoc localOnsubmit;
34     private String JavaDoc onreset;
35     protected String JavaDoc localOnreset;
36     private Map JavaDoc childParams = new HashMap JavaDoc();
37
38
39     /**
40      * Retrieves the tag's action attribute
41      *
42      * @return Returns the tag's action attribute
43      */

44     public String JavaDoc getAction() {
45         return action;
46     }
47
48     /**
49      * Populates the tag's action attribute
50      *
51      * @param action The value of the tag's action attribute
52      */

53     public void setAction(String JavaDoc action) {
54         this.action = action;
55     }
56
57     /**
58      * Retrieves the tags method attribute
59      *
60      * @return Returns the tags method attribute
61      */

62     public String JavaDoc getMethod() {
63         return method;
64     }
65
66     /**
67      * Populates the tags method attribute
68      *
69      * @param method The value of the tags method attribute
70      */

71     public void setMethod(String JavaDoc method) {
72         this.method = method;
73     }
74
75     /**
76      * Retrieves the tags onsubmit attribute
77      *
78      * @return Returns the tags onsubmit attribute
79      */

80     public String JavaDoc getOnsubmit() {
81         return onsubmit;
82     }
83
84     /**
85      * Populates the tags onsubmit attribute
86      *
87      * @param onsubmit The value of the tags onsubmit attribute
88      */

89     public void setOnsubmit(String JavaDoc onsubmit) {
90         this.onsubmit = onsubmit;
91     }
92
93     /**
94      * Retrieves the tags onreset attribute
95      *
96      * @return Returns the tags onreset attribute
97      */

98     public String JavaDoc getOnreset() {
99         return onreset;
100     }
101
102     /**
103      * Populates the tags onreset attribute
104      *
105      * @param onreset The value of the tags onreset attribute
106      */

107     public void setOnreset(String JavaDoc onreset) {
108         this.onreset = onreset;
109     }
110
111     /**
112      * Retrieves a stored attribute in this Form. These values are only valid
113      * within the block of the form tags
114      *
115      * @param key The key that the attribute was stored under
116      * @return The attribute or null if there is not one
117      */

118     public Object JavaDoc getAttribute(String JavaDoc key) {
119         return childParams.get(key);
120     }
121
122     /**
123      * Stores an attribute in this Form. These values are only valid within the
124      * block of the form tags
125      *
126      * @param key The key that the attribute will be stored under
127      * @param value The attributes value
128      */

129     public void setAttribute(String JavaDoc key, Object JavaDoc value) {
130         childParams.put(key, value);
131     }
132
133     /**
134      * Expands the attributes if not JSP 2.0 or greater.
135      *
136      * @throws JspException If there was any problems expanding the attributes
137      */

138     protected void initialize() throws JspException JavaDoc {
139         super.initialize();
140
141         localAction = action;
142         localMethod = method;
143         localOnsubmit = onsubmit;
144         localOnreset = onreset;
145
146         // Expands the attributes if not JSP 2.0 or greater
147
if (!JspTools.JSP_20) {
148             localAction = (String JavaDoc) JspTools.expand("action", action, String JavaDoc.class,
149                 this, pageContext);
150             localMethod = (String JavaDoc) JspTools.expand("method", method, String JavaDoc.class,
151                 this, pageContext);
152             localOnsubmit = (String JavaDoc) JspTools.expand("onsubmit", onsubmit,
153                 String JavaDoc.class, this, pageContext);
154             localOnreset = (String JavaDoc) JspTools.expand("onreset", onreset,
155                 String JavaDoc.class, this, pageContext);
156         }
157
158         attributes.put(HtmlConstants.ON_SUBMIT, localOnsubmit);
159         attributes.put(HtmlConstants.ON_RESET, localOnreset);
160     }
161
162     /**
163      * Outputs the opening form tag with the name, method and attributes given
164      * supplied on the JSP page.
165      *
166      * @return Always returns EVAL_BODY_INCLUDE
167      * @throws JspException If there was a problem outputting the HTML
168      */

169     public int doStartTag() throws JspException JavaDoc {
170         initialize();
171
172         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
173         createForm(buf, getId(), localName, localAction, localMethod);
174
175         try {
176             pageContext.getOut().print(buf.toString());
177         } catch (IOException JavaDoc ioe) {
178             throw new JspException JavaDoc(ioe);
179         }
180
181         return EVAL_BODY_INCLUDE;
182     }
183
184     /**
185      * Does the work of rendering the HTML for the start of a form tag and
186      * appends it to the StringBuffer given.
187      *
188      * @param buf The StringBuffer to append to
189      * @param url The url action of the form
190      * @param name The name of the form
191      * @param method The submission method of the form
192      * @throws JspException For overriding sub-classes
193      */

194     protected void createForm(StringBuffer JavaDoc buf, String JavaDoc id, String JavaDoc name, String JavaDoc url,
195             String JavaDoc method)
196     throws JspException JavaDoc {
197         if (url != null) {
198             StringBuffer JavaDoc localBuf = new StringBuffer JavaDoc();
199             appendContextPlusURL(localBuf, url);
200             url = localBuf.toString();
201         }
202
203         HtmlViewToolkit.createFormStartTag(buf, url, id, name, method,
204             attributes, singleAttrs);
205     }
206
207     /**
208      * Output the /form tag for this form.
209      *
210      * @return Always return EVAL_PAGE
211      */

212     public int doEndTag() throws JspException JavaDoc {
213         try {
214             pageContext.getOut().print(HtmlConstants.FORM_END_TAG);
215         } catch (IOException JavaDoc ioe) {
216             throw new JspException JavaDoc(ioe.toString());
217         }
218
219         // Clear out the child parameters map
220
childParams.clear();
221
222         return EVAL_PAGE;
223     }
224 }
Popular Tags