Source code: com/RuntimeCollective/webapps/tag/SubmitTag.java
1 /* $Header: /home/CVS/rjp/src/com/RuntimeCollective/webapps/tag/SubmitTag.java,v 1.11 2003/09/30 15:13:18 joe Exp $
2 * $Revision: 1.11 $
3 * $Date: 2003/09/30 15:13:18 $
4 *
5 * ====================================================================
6 *
7 * Josephine : http://www.runtime-collective.com/josephine/index.html
8 *
9 * Copyright (C) 2003 Runtime Collective
10 *
11 * This product includes software developed by the
12 * Apache Software Foundation (http://www.apache.org/).
13 *
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 *
28 */
29
30 package com.RuntimeCollective.webapps.tag;
31
32 import java.io.IOException;
33 import java.io.PrintStream;
34
35 import javax.servlet.jsp.JspWriter;
36 import javax.servlet.jsp.PageContext;
37 import javax.servlet.jsp.JspException;
38 import javax.servlet.jsp.JspTagException;
39 import javax.servlet.jsp.tagext.Tag;
40 import javax.servlet.jsp.tagext.TagSupport;
41 import javax.servlet.http.HttpServletRequest;
42 import javax.servlet.http.HttpServletResponse;
43
44 import org.apache.struts.action.Action;
45 import org.apache.commons.beanutils.BeanUtils;
46 import org.apache.struts.util.RequestUtils;
47 import org.apache.struts.util.ResponseUtils;
48 import org.apache.struts.taglib.html.BaseHandlerTag;
49
50 /** A custom JSP tag that produces a submit button whose label is different from the submitted value.
51 * Note this tag will not work if it sits next to a Struts html:submit tag.
52 * Convert other tags to rs:submit, to make sure it all works fine!
53 * <p>
54 * This tag takes the following attributes:
55 * <ul>
56 * <li> <code> name </code> - The name of the current form. [Mandatory]. </li>
57 * <li> <code> label </code> - The label of the submit button. [Mandatory]. </li>
58 * <li> <code> value </code> - The value of property to submit. Defaults to property's current value. [Optional]. </li>
59 * <li> <code> property </code> - The property to be set to value. Defaults to "formAction". [Optional].
60 * <li> <code> defaultValue </code> - The default value to submit, used if the user presses Enter. Defaults to "dummy". [Optional].
61 * <br>
62 * <strong>Note</strong> a javascript bug means that property must <strong>not</strong> be set to "submit" or "action". </li>
63 * <li> <code> paramName </code> - The name of a bean whose paramProperty will be appended to the value of the property to submit, with a blank space in front. [Optional]. </li>
64 * <li> <code> paramProperty </code> - The bean property which will be appended to the value of the property to submit, with a blank space in front. paramName must be set. Defaults to using the toString() value of paramBean. [Optional].
65 * <li> <code> confirm </code> - If this is set, then it will be used as the confirmation message in a popup window with 'OK/Cancel' buttons. The form will only be submitted if the user hits 'OK'. Defaults to "" (i.e. no confirmation required) [Optional]. </li>
66 * <li> <code> htmlJSInput </code> - You must set this to true if the form contains an <code>htmlJSInput</code> tag [Optional]. </li>
67
68 * </ul>
69 * This allows you to either specify the value explicitly, or take it from a bean's property.
70 * <p><strong>Note</strong> this tag must be used inside a form.
71 * <p><b>Examples:</b>
72 * <blockquote><code><rs:submit name="lessonForm" label="Save changes to this lesson"/></code></blockquote>
73 * The submitted form will contain a parameter "formAction" (the default) that has the existing value of <code>lessonForm</code>'s "formAction" property.
74 * <p>
75 * <blockquote><code><rs:submit name="lessonForm" label="Delete this lesson" value="delete"/></code></blockquote>
76 * The submitted form will contain a parameter "formAction" (the default) that has the value "delete".
77 *
78 * @version $Id: SubmitTag.java,v 1.11 2003/09/30 15:13:18 joe Exp $
79 */
80 public class SubmitTag extends BaseHandlerTag {
81
82 // == Properties ===================================================
83
84
85 /** The label of the submit button. */
86 protected String label = "";
87 /** Get the label of the submit button. */
88 public String getLabel() { return this.label; }
89 /** Set the label of the submit button. */
90 public void setLabel(String label) { this.label = label; }
91
92 /** The value of property to submit. */
93 protected String value = "";
94 /** Get the value of property to submit. */
95 public String getValue() { return this.value; }
96 /** Set the value of property to submit. */
97 public void setValue(String value) { this.value = value; }
98
99 /** The property to be set to value. */
100 protected String property = "formAction";
101 /** Get the property to be set to value. */
102 public String getProperty() { return this.property; }
103 /** Set the property to be set to value. */
104 public void setProperty(String property) { this.property = property; }
105
106 /** The name of the form. */
107 protected String name = "";
108 /** Get the name of the form. */
109 public String getName() { return this.name; }
110 /** Set the name of the form. */
111 public void setName(String name) { this.name = name; }
112
113 /** The defaultValue of the form. */
114 protected String defaultValue = "dummy";
115 /** Get the defaultValue of the form. */
116 public String getDefaultValue() { return this.defaultValue; }
117 /** Set the defaultValue of the form. */
118 public void setDefaultValue(String defaultValue) { this.defaultValue = defaultValue; }
119
120 /** The confirmation message. */
121 protected String confirm = "";
122 /** Get the confirmation message. */
123 public String getConfirm() { return this.confirm; }
124 /** Set the confirmation message. */
125 public void setConfirm(String confirm) { this.confirm = confirm; }
126
127 /** Whether this form contains an htmlJSInput tag. */
128 protected String htmlJSInput = "false";
129 /** Get whether this form contains an htmlJSInput tag. */
130 public String getHtmlJSInput() { return this.htmlJSInput; }
131 /** Set whether this form contains an htmlJSInput tag. */
132 public void setHtmlJSInput(String htmlJSInput) { this.htmlJSInput = htmlJSInput; }
133
134 /** The paramName of the form. */
135 protected String paramName = null;
136 /** Get the paramName of the form. */
137 public String getParamName() { return this.paramName; }
138 /** Set the paramName of the form. */
139 public void setParamName(String paramName) { this.paramName = paramName; }
140
141 /** The paramProperty to be set to value. */
142 protected String paramProperty = null;
143 /** Get the paramProperty to be set to value. */
144 public String getParamProperty() { return this.paramProperty; }
145 /** Set the paramProperty to be set to value. */
146 public void setParamProperty(String paramProperty) { this.paramProperty = paramProperty; }
147
148
149 // == Tag methods ==================================
150
151 public int doStartTag() throws JspException {
152
153 // If the value is null, try and get it from
154 // the named bean's property
155 if ( value.equals("") ) {
156 Object bean = pageContext.findAttribute(name);
157 if (bean == null)
158 throw new JspException ("Can't access bean! "+name);
159 try {
160 String beanValue = BeanUtils.getSimpleProperty(bean, property);
161 if (beanValue == null)
162 beanValue = "";
163 setValue(beanValue);
164 } catch (Exception e) {
165 throw new JspException ("Error in submit tag: "+e);
166 }
167 }
168
169 // append the paramName's paramProperty if it has been specified
170 if (paramName != null && !paramName.equals("")) {
171 Object paramBean = pageContext.findAttribute(paramName);
172 if (paramBean == null)
173 throw new JspException ("Can't access paramBean! "+paramName);
174 try {
175 String paramBeanValue;
176 if (paramProperty == null || paramProperty.equals(""))
177 paramBeanValue = paramBean.toString();
178 else
179 paramBeanValue = BeanUtils.getSimpleProperty(paramBean, paramProperty);
180 if (paramBeanValue == null)
181 paramBeanValue = "";
182 setValue(this.getValue()+" "+paramBeanValue);
183 } catch (Exception e) {
184 throw new JspException ("Error in submit tag: "+e);
185 }
186 }
187
188 //results.append(ResponseUtils.filter(value));
189
190 StringBuffer html = new StringBuffer(600);
191 String submitnameproperty = (new StringBuffer(60)).append("submit_").append(name).append("_").append(property).toString();
192
193 // first check whether this is the first submit button for this form property
194
195 // FR: this used to be set on the pageContent, which was failing for forms which
196 // include sub-templates. use the request instead.
197 HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
198
199 if (request.getAttribute(submitnameproperty) == null) {
200
201 // create the javascript for the submit function for this form.
202 html.append("\n<script language=\"javascript\" type=\"text/javascript\"><!-- \n");
203 html.append(" function ").append(submitnameproperty).append("(newValue,message) { \n");
204 html.append(" document.").append(name).append(".").append(property).append(".value=newValue;\n");
205
206 // If this form contains an htmlJSInput tag, we also need to call its JavaScript method
207 if (htmlJSInput.equals("true")) {
208 html.append("getHTMLEditorAppletValue()\n");
209 }
210
211 html.append(" if ( message==\"\" ) { document.").append(name).append(".submit(); }\n");
212 html.append(" else if ( confirm(message) ) { document.").append(name).append(".submit(); }\n");
213 html.append(" }\n");
214 html.append("//--></script>\n");
215
216 // Add the attribute we're changing, with a dummy value
217 html.append("<input type=\"hidden\" name=\"").append(property).append("\" value=\"").append(defaultValue).append("\" />\n");
218 }
219
220 // add the button code.
221 html.append("<input type=\"button\" value=\"").append(label).append("\" onclick=\"").append(submitnameproperty).append("('").append(value).append("','").append(confirm).append("');\"").append(prepareStyles()).append(prepareEventHandlers()).append(" />");
222
223 // write the code.
224 try {
225 pageContext.getOut().println( html.toString() );
226 } catch (IOException e) {
227 throw new JspTagException("I/O Exception " + e.getMessage() );
228 }
229
230 // and add the page context flag indicating that the javascript for this property is available
231 request.setAttribute(submitnameproperty, "true");
232
233 return SKIP_BODY;
234 }
235
236
237 public void release() {
238 super.release();
239 property = "formAction";
240 confirm = "";
241 name = "";
242 label = "";
243 value = "";
244 paramName = null;
245 paramProperty = null;
246 }
247 }
248
249
250
251
252
253
254
255
256