KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > taglib > core > html > SubmitTag


1 /*
2  * Copyright 2005 Blandware (http://www.blandware.com)
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 com.blandware.atleap.webapp.taglib.core.html;
17
18 import org.apache.struts.taglib.TagUtils;
19
20 import javax.servlet.jsp.JspException JavaDoc;
21 import javax.servlet.jsp.PageContext JavaDoc;
22 import javax.servlet.jsp.tagext.JspFragment JavaDoc;
23 import java.io.StringWriter JavaDoc;
24 import java.io.IOException JavaDoc;
25
26 import com.blandware.atleap.webapp.taglib.core.BaseHandlerTag;
27
28
29 /**
30  * <p>Tag for 'submit' element. Originally taken from Struts SubmitTag.</p>
31  * <p>
32  * Following attributes are supported:
33  * <ul>
34  * <li>
35  * <b>property</b> - name of the generated input field
36  * </li>
37  * <li>
38  * <b>value</b> - value of the button label
39  * </li>
40  * <li>
41  * <b>disableButtonOnSubmit</b> - whether to disable submit button after it was pressed
42  * to prevent multiple submittion. This can be 'true' or 'false',
43  * default is 'false'.
44  * </li>
45  * </ul>
46  * Some attributes are inherited from {@link BaseHandlerTag}.
47  * </p>
48  * <p>
49  * This tag can also have body, its content will be rendered as submit button
50  * label. <b>Value</b> has precedence over body. If none of <b>value</b> and
51  * tag body has been specified, button label will be 'Submit'.
52  * </p>
53  *
54  * <p><a HREF="SubmitTag.java.htm"><i>View source</i></a></p>
55  *
56  * @author Roman Puchkovskiy <a HREF="mailto:roman.puchkovskiy@blandware.com">
57  * &lt;roman.puchkovskiy@blandware.com&gt;</a>
58  * @version $Revision: 1.3 $ $Date: 2005/09/21 13:46:02 $
59  * @see com.blandware.atleap.webapp.taglib.core.BaseHandlerTag
60  * @see org.apache.struts.taglib.html.SubmitTag
61  * @jsp.tag name="submit"
62  * body-content="scriptless"
63  */

64 public class SubmitTag extends BaseHandlerTag {
65     /**
66      * The name of the generated input field.
67      */

68     protected String JavaDoc property = null;
69
70     /**
71      * The value of the button label.
72      */

73     protected String JavaDoc value = null;
74
75     /**
76      * Whether to disable submit button after pressing it (to prevent multiple
77      * submittion)
78      */

79     protected Boolean JavaDoc disableButtonOnSubmit = Boolean.FALSE;
80
81     /**
82      * Returns the property.
83      *
84      * @return property, which is the name of corresponding 'input' tag
85      * @jsp.attribute required="false"
86      * rtexprvalue="true"
87      * type="java.lang.String"
88      * description="Name of the 'input' tag"
89      */

90     public String JavaDoc getProperty() {
91
92         return (this.property);
93
94     }
95
96     /**
97      * Sets the property name.
98      *
99      * @param property The property name
100      */

101     public void setProperty(String JavaDoc property) {
102
103         this.property = property;
104
105     }
106
107     /**
108      * Returns the label value.
109      *
110      * @return value of the label
111      * @jsp.attribute required="false"
112      * rtexprvalue="true"
113      * type="java.lang.String"
114      * description="Label of the button"
115      */

116     public String JavaDoc getValue() {
117
118         return (this.value);
119
120     }
121
122     /**
123      * Sets the label value.
124      *
125      * @param value The label value
126      */

127     public void setValue(String JavaDoc value) {
128
129         this.value = value;
130
131     }
132
133     /**
134      * Gets a property specifiing whether to disable button after submit
135      *
136      * @return whether to disable button after submit
137      * @jsp.attribute required="false"
138      * rtexprvalue="true"
139      * type="java.lang.Boolean"
140      * description="Whether to disable button after submit"
141      */

142     public Boolean JavaDoc getDisableButtonOnSubmit() {
143         return disableButtonOnSubmit;
144     }
145
146     /**
147      * Sets whether to disable button after submit
148      *
149      * @param disableButtonOnSubmit whether to disable button after submit
150      */

151     public void setDisableButtonOnSubmit(Boolean JavaDoc disableButtonOnSubmit) {
152         this.disableButtonOnSubmit = disableButtonOnSubmit;
153     }
154
155     /**
156      * Processes the tag.
157      *
158      * @throws javax.servlet.jsp.JspException if a JSP exception has occurred
159      * @throws java.io.IOException is an IO exception has occured
160      */

161     public void doTag() throws JspException JavaDoc, IOException JavaDoc {
162         // Get text from inside of the tag
163
String JavaDoc text;
164         StringWriter JavaDoc writer = new StringWriter JavaDoc();
165         JspFragment JavaDoc body = getJspBody();
166         if (body != null) {
167             body.invoke(writer);
168             text = writer.toString().trim();
169         } else {
170             text = null;
171         }
172
173         // Acquire the label value we will be generating
174
String JavaDoc label = value;
175         if ((label == null) && (text != null)) {
176             label = text;
177         }
178         if ((label == null) || (label.length() < 1)) {
179             label = "Submit";
180         }
181
182         // Prepend disabling JS-code to onsubmit event handler, if needed
183
String JavaDoc disablingCode = "this.disabled = true;";
184         if (disableButtonOnSubmit == null) {
185             disableButtonOnSubmit = Boolean.FALSE;
186         }
187         if (disableButtonOnSubmit.booleanValue()) {
188             if (onclick == null) {
189                 onclick = disablingCode;
190             } else {
191                 onclick = disablingCode + onclick;
192             }
193         }
194
195         // Generate an HTML element
196
StringBuffer JavaDoc results = new StringBuffer JavaDoc();
197         results.append("<input type=\"submit\"");
198         if (property != null) {
199             results.append(" name=\"");
200             results.append(property);
201             results.append("\" ");
202         }
203         if (label != null) {
204             results.append(" value=\"");
205             results.append(label);
206             results.append("\" ");
207         }
208         results.append(prepareAttributes());
209         results.append("/>");
210
211         // Write HTML-code to page
212
PageContext JavaDoc pageContext = (PageContext JavaDoc) getJspContext();
213         TagUtils.getInstance().write(pageContext, results.toString());
214     }
215 }
216
Popular Tags