View Javadoc

1   /*
2    * $Id: SubmitTag.java 376841 2006-02-10 21:01:28Z husted $
3    *
4    * Copyright 1999-2004 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts.taglib.html;
19  
20  import org.apache.struts.taglib.TagUtils;
21  import org.apache.struts.util.MessageResources;
22  
23  import javax.servlet.jsp.JspException;
24  
25  /***
26   * Tag for input fields of type "submit".
27   *
28   * @version $Rev: 376841 $ $Date: 2005-04-06 02:22:39 -0400 (Wed, 06 Apr 2005)
29   *          $
30   */
31  public class SubmitTag extends BaseHandlerTag {
32      // ----------------------------------------------------- Instance Variables
33  
34      /***
35       * The message resources for this package.
36       */
37      protected static MessageResources messages =
38          MessageResources.getMessageResources(Constants.Package
39              + ".LocalStrings");
40  
41      /***
42       * The name of the generated input field.
43       */
44      protected String property = null;
45  
46      /***
47       * The body content of this tag (if any).
48       */
49      protected String text = null;
50  
51      /***
52       * The value of the button label.
53       */
54      protected String value = null;
55  
56      // ------------------------------------------------------------- Properties
57  
58      /***
59       * Return the property.
60       */
61      public String getProperty() {
62          return (this.property);
63      }
64  
65      /***
66       * Set the property name.
67       *
68       * @param property The property name
69       */
70      public void setProperty(String property) {
71          this.property = property;
72      }
73  
74      /***
75       * Return the label value.
76       */
77      public String getValue() {
78          return (this.value);
79      }
80  
81      /***
82       * Set the label value.
83       *
84       * @param value The label value
85       */
86      public void setValue(String value) {
87          this.value = value;
88      }
89  
90      // --------------------------------------------------------- Public Methods
91  
92      /***
93       * Process the start of this tag.
94       *
95       * @throws JspException if a JSP exception has occurred
96       */
97      public int doStartTag() throws JspException {
98          // Do nothing until doEndTag() is called
99          this.text = null;
100 
101         return (EVAL_BODY_TAG);
102     }
103 
104     /***
105      * Save the associated label from the body content.
106      *
107      * @throws JspException if a JSP exception has occurred
108      */
109     public int doAfterBody() throws JspException {
110         if (bodyContent != null) {
111             String value = bodyContent.getString().trim();
112 
113             if (value.length() > 0) {
114                 text = value;
115             }
116         }
117 
118         return (SKIP_BODY);
119     }
120 
121     /***
122      * Process the end of this tag. <p> Support for Indexed property since
123      * Struts 1.1
124      *
125      * @throws JspException if a JSP exception has occurred
126      */
127     public int doEndTag() throws JspException {
128         // Generate an HTML element
129         StringBuffer results = new StringBuffer();
130 
131         results.append(getElementOpen());
132         prepareAttribute(results, "name", prepareName());
133         prepareButtonAttributes(results);
134         results.append(prepareEventHandlers());
135         results.append(prepareStyles());
136         prepareOtherAttributes(results);
137         results.append(getElementClose());
138 
139         TagUtils.getInstance().write(pageContext, results.toString());
140 
141         return (EVAL_PAGE);
142     }
143 
144     /***
145      * Render the opening element.
146      *
147      * @return The opening part of the element.
148      */
149     protected String getElementOpen() {
150         return "<input type=\"submit\"";
151     }
152 
153     /***
154      * Prepare the name element
155      *
156      * @return The element name.
157      */
158     protected String prepareName()
159         throws JspException {
160         if (property == null) {
161             return null;
162         }
163 
164         // * @since Struts 1.1
165         if (indexed) {
166             StringBuffer results = new StringBuffer();
167 
168             results.append(property);
169             prepareIndex(results, null);
170 
171             return results.toString();
172         }
173 
174         return property;
175     }
176 
177     /***
178      * Render the button attributes
179      *
180      * @param results The StringBuffer that output will be appended to.
181      */
182     protected void prepareButtonAttributes(StringBuffer results)
183         throws JspException {
184         prepareAttribute(results, "accesskey", getAccesskey());
185         prepareAttribute(results, "tabindex", getTabindex());
186         prepareValue(results);
187     }
188 
189     /***
190      * Render the value element
191      *
192      * @param results The StringBuffer that output will be appended to.
193      */
194     protected void prepareValue(StringBuffer results) {
195         // Acquire the label value we will be generating
196         String label = value;
197 
198         if ((label == null) && (text != null)) {
199             label = text;
200         }
201 
202         if ((label == null) || (label.length() < 1)) {
203             label = getDefaultValue();
204         }
205 
206         prepareAttribute(results, "value", label);
207     }
208 
209     /***
210      * Return the default value.
211      *
212      * @return The default value if none supplied.
213      */
214     protected String getDefaultValue() {
215         return "Submit";
216     }
217 
218     /***
219      * Release any acquired resources.
220      */
221     public void release() {
222         super.release();
223         property = null;
224         text = null;
225         value = null;
226     }
227 }