View Javadoc

1   /*
2    * $Id: Submit.java 557348 2007-07-18 18:15:37Z jholmes $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.struts2.components;
22  
23  import java.io.Writer;
24  
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.http.HttpServletResponse;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.apache.struts2.views.annotations.StrutsTag;
31  import org.apache.struts2.views.annotations.StrutsTagAttribute;
32  
33  import com.opensymphony.xwork2.util.ValueStack;
34  
35  /***
36   * <!-- START SNIPPET: javadoc -->
37   * Render a submit button. The submit tag is used together with the form tag to provide asynchronous form submissions.
38   * The submit can have three different types of rendering:
39   * <ul>
40   * <li>input: renders as html &lt;input type="submit"...&gt;</li>
41   * <li>image: renders as html &lt;input type="image"...&gt;</li>
42   * <li>button: renders as html &lt;button type="submit"...&gt;</li>
43   * </ul>
44   * Please note that the button type has advantages by adding the possibility to seperate the submitted value from the
45   * text shown on the button face, but has issues with Microsoft Internet Explorer at least up to 6.0
46   * <!-- END SNIPPET: javadoc -->
47   *
48   * <p/> <b>Examples</b>
49   * <pre>
50   * <!-- START SNIPPET: example -->
51   * &lt;s:submit value="%{'Submit the form'}" /&gt;
52   * <!-- END SNIPPET: example -->
53   * </pre>
54   * <pre>
55   * <!-- START SNIPPET: example2 -->
56   * Render an image submit:
57   * &lt;s:submit type="image" value="%{'Submit the form'}" src="submit.gif"/&gt;
58   * <!-- END SNIPPET: example2 -->
59   * </pre>
60   * <pre>
61   * <!-- START SNIPPET: example3 -->
62   * Render an button submit:
63   * &lt;s:submit type="button" value="%{'Submit the form'}"/&gt;
64   * <!-- END SNIPPET: example3 -->
65   * </pre>
66   *
67   * <!-- START SNIPPET: ajaxJavadoc -->
68   * <B>THE FOLLOWING IS ONLY VALID WHEN AJAX IS CONFIGURED</B>
69   * <ul>
70   *      <li>href</li>
71   *      <li>errorText</li>
72   *      <li>listenTopics</li>
73   *      <li>notifyTopics</li>
74   *      <li>executeScripts</li>
75   *      <li>loadingText</li>
76   *      <li>listenTopics</li>
77   *      <li>handler</li>
78   *      <li>formId</li>
79   *      <li>formFilter</li>
80   *      <li>targets</li>
81   *      <li>showErrorTransportText</li>
82   *      <li>targets</li>
83   *      <li>indicator</li>
84   * </ul>
85   * 'resultDivId' Deprecated. Use targets.</p>
86   * 'targets' is a list of element ids whose content will be updated with the
87   * text returned from request.<p/>
88   * 'errorText' is the text that will be displayed when there is an error making the request.<p/>
89   * 'onLoadJS' Deprecated. Use 'notifyTopics'.<p/>
90   * 'preInvokeJS' Deprecated. Use 'notifyTopics'.<p/>
91   * 'executeScripts' if set to true will execute javascript sections in the returned text.<p/>
92   * 'loadingText' is the text that will be displayed on the 'targets' elements while making the
93   * request.<p/>
94   * 'handler' is the name of the function that will take care of making the AJAX request. Dojo's widget
95   * and dom node are passed as parameters).<p/>
96   * 'formId' is the id of the html form whose fields will be seralized and passed as parameters
97   * in the request.<p/>
98   * 'formFilter' is the name of a function which will be used to filter the fields that will be
99   * seralized. This function takes as a parameter the element and returns true if the element
100  * should be included.<p/>
101  * 'listenTopics' comma separated list of topics names, that will trigger a request
102  * 'indicator' element to be shown while the request executing
103  * 'showErrorTransportText': whether errors should be displayed (on 'targets')</p>
104  * 'showLoadingText' show loading text on targets</p>
105  * 'notifyTopics' comma separated list of topics names, that will be published. Three parameters are passed:<p/>
106  * <ul>
107  *      <li>data: html or json object when type='load' or type='error'</li>
108  *      <li>type: 'before' before the request is made, 'load' when the request succeeds, or 'error' when it fails</li>
109  *      <li>request: request javascript object, when type='load' or type='error'</li>
110  * </ul>
111  * <!-- END SNIPPET: ajaxJavadoc -->
112  *
113  * <!-- START SNIPPET: ajxExDescription1 -->
114  * Show the results in another div. If you want your results to be shown in
115  * a div, use the resultDivId where the id is the id of the div you want them
116  * shown in. This is an inner HTML approah. Your results get jammed into
117  * the div for you. Here is a sample of this approach:
118  * <!-- END SNIPPET: ajxExDescription1 -->
119  *
120  * <pre>
121  * <!-- START SNIPPET: ajxExample1 -->
122  * Remote form replacing another div:
123  * &lt;div id='two' style="border: 1px solid yellow;"&gt;Initial content&lt;/div&gt;
124  * &lt;s:form
125  *       id='theForm2'
126  *       cssStyle="border: 1px solid green;"
127  *       action='/AjaxRemoteForm.action'
128  *       method='post'
129  *       theme="ajax"&gt;
130  *
131  *   &lt;input type='text' name='data' value='Struts User' /&gt;
132  *   &lt;s:submit value="GO2" theme="ajax" resultDivId="two" /&gt;
133  *
134  * &lt;/s:form &gt;
135  * <!-- END SNIPPET: ajxExample1 -->
136  * </pre>
137  *
138  */
139 @StrutsTag(name="submit", tldTagClass="org.apache.struts2.views.jsp.ui.SubmitTag", description="Render a submit button")
140 public class Submit extends FormButton implements RemoteUICallBean{
141     
142     private static final Log LOG = LogFactory.getLog(Submit.class);
143     
144     final public static String TEMPLATE = "submit";
145 
146     protected String href;
147     protected String errorText;
148     protected String executeScripts;
149     protected String loadingText;
150     protected String listenTopics;
151     protected String handler;
152     protected String formId;
153     protected String formFilter;
154     protected String src;
155     protected String notifyTopics;
156     protected String showErrorTransportText;
157     protected String indicator;
158     protected String showLoadingText;
159     //these two are called "preInvokeJS" and "onLoadJS" on the tld
160     //Names changed here to keep some consistency
161     protected String beforeLoading;
162     protected String afterLoading;
163 
164     //this one is called "resultDivId" on the tld
165     protected String targets;
166 
167     public Submit(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
168         super(stack, request, response);
169     }
170 
171     protected String getDefaultTemplate() {
172         return TEMPLATE;
173     }
174 
175     public void evaluateParams() {
176         if ((key == null) && (value == null)) {
177             value = "Submit";
178         }
179 
180         if (((key != null)) && (value == null)) {
181             this.value = "%{getText('"+key +"')}";
182         }
183 
184         super.evaluateParams();
185     }
186 
187     public void evaluateExtraParams() {
188         super.evaluateExtraParams();
189 
190         if (href != null)
191             addParameter("href", findString(href));
192         if (errorText != null)
193             addParameter("errorText", findString(errorText));
194         if (loadingText != null)
195             addParameter("loadingText", findString(loadingText));
196         if (afterLoading != null)
197             addParameter("afterLoading", findString(afterLoading));
198         if (beforeLoading != null)
199             addParameter("beforeLoading", findString(beforeLoading));
200         if (executeScripts != null)
201             addParameter("executeScripts", findValue(executeScripts, Boolean.class));
202         if (listenTopics != null)
203             addParameter("listenTopics", findString(listenTopics));
204         if (notifyTopics != null)
205             addParameter("notifyTopics", findString(notifyTopics));
206         if (handler != null)
207             addParameter("handler", findString(handler));
208         if (formId != null)
209             addParameter("formId", findString(formId));
210         if (formFilter != null)
211             addParameter("formFilter", findString(formFilter));
212         if (src != null)
213             addParameter("src", findString(src));
214         if (indicator != null)
215             addParameter("indicator", findString(indicator));
216         if (targets != null)
217             addParameter("targets", findString(targets));
218         if (showLoadingText != null)
219             addParameter("showLoadingText", findString(showLoadingText));
220     }
221 
222     /***
223      * Indicate whether the concrete button supports the type "image".
224      *
225      * @return <tt>true</tt> to indicate type image is supported.
226      */
227     protected boolean supportsImageType() {
228         return true;
229     }
230     
231     /***
232      * Overrides to be able to render body in a template rather than always before the template
233      */
234     public boolean end(Writer writer, String body) {
235         evaluateParams();
236         try {
237             addParameter("body", body);
238             
239             mergeTemplate(writer, buildTemplateName(template, getDefaultTemplate()));
240         } catch (Exception e) {
241             LOG.error("error when rendering", e);
242         }
243         finally {
244             popComponentStack();
245         }
246 
247         return false;
248     }
249 
250     @StrutsTagAttribute(description="Topic that will trigger the remote call")
251     public void setListenTopics(String listenTopics) {
252         this.listenTopics = listenTopics;
253     }
254 
255     @StrutsTagAttribute(description="The theme to use for the element. <b>This tag will usually use the ajax theme.</b>")
256     public void setTheme(String theme) {
257         super.setTheme(theme);
258     }
259 
260     @StrutsTagAttribute(description="The URL to call to obtain the content. Note: If used with ajax context, the value must be set as an url tag value.")
261     public void setHref(String href) {
262         this.href = href;
263     }
264 
265     @StrutsTagAttribute(description="The text to display to the user if the is an error fetching the content")
266     public void setErrorText(String errorText) {
267         this.errorText = errorText;
268     }
269 
270     @StrutsTagAttribute(name="onLoadJS", description="Deprecated. Use 'notifyTopics'. Javascript code execute after reload")
271     public void setAfterLoading(String afterLoading) {
272         this.afterLoading = afterLoading;
273     }
274 
275 
276     @StrutsTagAttribute(name="preInvokeJS", description="Deprecated. Use 'notifyTopics'. Javascript code execute before reload")
277     public void setBeforeLoading(String beforeLoading) {
278         this.beforeLoading = beforeLoading;
279     }
280 
281     @StrutsTagAttribute(description="Javascript code in the fetched content will be executed", type="Boolean", defaultValue="false")
282     public void setExecuteScripts(String executeScripts) {
283         this.executeScripts = executeScripts;
284     }
285 
286     @StrutsTagAttribute(description="Text to be shown while content is being fetched", defaultValue="Loading...")
287     public void setLoadingText(String loadingText) {
288         this.loadingText = loadingText;
289     }
290 
291     @StrutsTagAttribute(description="Javascript function name that will make the request")
292     public void setHandler(String handler) {
293         this.handler = handler;
294     }
295 
296     @StrutsTagAttribute(description="Function name used to filter the fields of the form.")
297     public void setFormFilter(String formFilter) {
298         this.formFilter = formFilter;
299     }
300 
301     @StrutsTagAttribute(description="Form id whose fields will be serialized and passed as parameters")
302     public void setFormId(String formId) {
303         this.formId = formId;
304     }
305 
306     @StrutsTagAttribute(description="Supply an image src for <i>image</i> type submit button. Will have no effect for types <i>input</i> and <i>button</i>.")
307     public void setSrc(String src) {
308         this.src = src;
309     }
310 
311     @StrutsTagAttribute(description="Comma delimited list of ids of the elements whose content will be updated")
312     public void setTargets(String targets) {
313         this.targets = targets;
314     }
315 
316 
317     @StrutsTagAttribute(description="Topics that will published when the remote call completes")
318     public void setNotifyTopics(String notifyTopics) {
319         this.notifyTopics = notifyTopics;
320     }
321 
322 
323     @StrutsTagAttribute(description="Set whether errors will be shown or not", type="Boolean", defaultValue="true")
324     public void setShowErrorTransportText(String showErrorTransportText) {
325         this.showErrorTransportText = showErrorTransportText;
326     }
327 
328 
329     @StrutsTagAttribute(description="Set indicator")
330     public void setIndicator(String indicator) {
331         this.indicator = indicator;
332     }
333 
334     @StrutsTagAttribute(description="Show loading text on targets", type="Boolean", defaultValue="true")
335     public void setShowLoadingText(String showLoadingText) {
336         this.showLoadingText = showLoadingText;
337     }
338 }