001: /*
002: * $Id: SubmitTag.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts.taglib.html;
022:
023: import org.apache.struts.taglib.TagUtils;
024: import org.apache.struts.util.MessageResources;
025:
026: import javax.servlet.jsp.JspException;
027:
028: /**
029: * Tag for input fields of type "submit".
030: *
031: * @version $Rev: 471754 $ $Date: 2005-04-06 02:22:39 -0400 (Wed, 06 Apr 2005)
032: * $
033: */
034: public class SubmitTag extends BaseHandlerTag {
035: // ----------------------------------------------------- Instance Variables
036:
037: /**
038: * The message resources for this package.
039: */
040: protected static MessageResources messages = MessageResources
041: .getMessageResources(Constants.Package + ".LocalStrings");
042:
043: /**
044: * The name of the generated input field.
045: */
046: protected String property = null;
047:
048: /**
049: * The body content of this tag (if any).
050: */
051: protected String text = null;
052:
053: /**
054: * The value of the button label.
055: */
056: protected String value = null;
057:
058: // ------------------------------------------------------------- Properties
059:
060: /**
061: * Return the property.
062: */
063: public String getProperty() {
064: return (this .property);
065: }
066:
067: /**
068: * Set the property name.
069: *
070: * @param property The property name
071: */
072: public void setProperty(String property) {
073: this .property = property;
074: }
075:
076: /**
077: * Return the label value.
078: */
079: public String getValue() {
080: return (this .value);
081: }
082:
083: /**
084: * Set the label value.
085: *
086: * @param value The label value
087: */
088: public void setValue(String value) {
089: this .value = value;
090: }
091:
092: // --------------------------------------------------------- Public Methods
093:
094: /**
095: * Process the start of this tag.
096: *
097: * @throws JspException if a JSP exception has occurred
098: */
099: public int doStartTag() throws JspException {
100: // Do nothing until doEndTag() is called
101: this .text = null;
102:
103: return (EVAL_BODY_TAG);
104: }
105:
106: /**
107: * Save the associated label from the body content.
108: *
109: * @throws JspException if a JSP exception has occurred
110: */
111: public int doAfterBody() throws JspException {
112: if (bodyContent != null) {
113: String value = bodyContent.getString().trim();
114:
115: if (value.length() > 0) {
116: text = value;
117: }
118: }
119:
120: return (SKIP_BODY);
121: }
122:
123: /**
124: * Process the end of this tag. <p> Support for Indexed property since
125: * Struts 1.1
126: *
127: * @throws JspException if a JSP exception has occurred
128: */
129: public int doEndTag() throws JspException {
130: // Generate an HTML element
131: StringBuffer results = new StringBuffer();
132:
133: results.append(getElementOpen());
134: prepareAttribute(results, "name", prepareName());
135: prepareButtonAttributes(results);
136: results.append(prepareEventHandlers());
137: results.append(prepareStyles());
138: prepareOtherAttributes(results);
139: results.append(getElementClose());
140:
141: TagUtils.getInstance().write(pageContext, results.toString());
142:
143: return (EVAL_PAGE);
144: }
145:
146: /**
147: * Render the opening element.
148: *
149: * @return The opening part of the element.
150: */
151: protected String getElementOpen() {
152: return "<input type=\"submit\"";
153: }
154:
155: /**
156: * Prepare the name element
157: *
158: * @return The element name.
159: */
160: protected String prepareName() throws JspException {
161: if (property == null) {
162: return null;
163: }
164:
165: // * @since Struts 1.1
166: if (indexed) {
167: StringBuffer results = new StringBuffer();
168:
169: results.append(property);
170: prepareIndex(results, null);
171:
172: return results.toString();
173: }
174:
175: return property;
176: }
177:
178: /**
179: * Render the button attributes
180: *
181: * @param results The StringBuffer that output will be appended to.
182: */
183: protected void prepareButtonAttributes(StringBuffer results)
184: throws JspException {
185: prepareAttribute(results, "accesskey", getAccesskey());
186: prepareAttribute(results, "tabindex", getTabindex());
187: prepareValue(results);
188: }
189:
190: /**
191: * Render the value element
192: *
193: * @param results The StringBuffer that output will be appended to.
194: */
195: protected void prepareValue(StringBuffer results) {
196: // Acquire the label value we will be generating
197: String label = value;
198:
199: if ((label == null) && (text != null)) {
200: label = text;
201: }
202:
203: if ((label == null) || (label.length() < 1)) {
204: label = getDefaultValue();
205: }
206:
207: prepareAttribute(results, "value", label);
208: }
209:
210: /**
211: * Return the default value.
212: *
213: * @return The default value if none supplied.
214: */
215: protected String getDefaultValue() {
216: return "Submit";
217: }
218:
219: /**
220: * Release any acquired resources.
221: */
222: public void release() {
223: super.release();
224: property = null;
225: text = null;
226: value = null;
227: }
228: }
|