View Javadoc

1   /*
2    * $Id: Submit.java 501762 2007-01-31 08:25:24Z mrdon $
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'}" /&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'}" label="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'}" label="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  * 'notifyTopics' comma separated list of topics names, that will be published. Three parameters are passed:<p/>
105  * <ul>
106  *      <li>data: html or json object when type='load' or type='error'</li>
107  *      <li>type: 'before' before the request is made, 'load' when the request succeeds, or 'error' when it fails</li>
108  *      <li>request: request javascript object, when type='load' or type='error'</li>
109  * </ul>
110  * <!-- END SNIPPET: ajaxJavadoc -->
111  *
112  * <!-- START SNIPPET: ajxExDescription1 -->
113  * Show the results in another div. If you want your results to be shown in
114  * a div, use the resultDivId where the id is the id of the div you want them
115  * shown in. This is an inner HTML approah. Your results get jammed into
116  * the div for you. Here is a sample of this approach:
117  * <!-- END SNIPPET: ajxExDescription1 -->
118  *
119  * <pre>
120  * <!-- START SNIPPET: ajxExample1 -->
121  * Remote form replacing another div:
122  * &lt;div id='two' style="border: 1px solid yellow;"&gt;Initial content&lt;/div&gt;
123  * &lt;s:form
124  *       id='theForm2'
125  *       cssStyle="border: 1px solid green;"
126  *       action='/AjaxRemoteForm.action'
127  *       method='post'
128  *       theme="ajax"&gt;
129  *
130  *   &lt;input type='text' name='data' value='Struts User' /&gt;
131  *   &lt;s:submit value="GO2" theme="ajax" resultDivId="two" /&gt;
132  *
133  * &lt;/s:form &gt;
134  * <!-- END SNIPPET: ajxExample1 -->
135  * </pre>
136  *
137  */
138 @StrutsTag(name="submit", tldTagClass="org.apache.struts2.views.jsp.ui.SubmitTag", description="Render a submit button")
139 public class Submit extends FormButton implements RemoteUICallBean{
140     
141     private static final Log LOG = LogFactory.getLog(Submit.class);
142     
143     final public static String TEMPLATE = "submit";
144 
145     protected String href;
146     protected String errorText;
147     protected String executeScripts;
148     protected String loadingText;
149     protected String listenTopics;
150     protected String handler;
151     protected String formId;
152     protected String formFilter;
153     protected String src;
154     protected String notifyTopics;
155     protected String showErrorTransportText;
156     protected String indicator;
157 
158     //these two are called "preInvokeJS" and "onLoadJS" on the tld
159     //Names changed here to keep some consistency
160     protected String beforeLoading;
161     protected String afterLoading;
162 
163     //this one is called "resultDivId" on the tld
164     protected String targets;
165 
166     public Submit(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
167         super(stack, request, response);
168     }
169 
170     protected String getDefaultTemplate() {
171         return TEMPLATE;
172     }
173 
174     public void evaluateParams() {
175         if ((key == null) && (value == null)) {
176             value = "Submit";
177         }
178 
179         if (((key != null)) && (value == null)) {
180             this.value = "%{getText('"+key +"')}";
181         }
182 
183         super.evaluateParams();
184     }
185 
186     public void evaluateExtraParams() {
187         super.evaluateExtraParams();
188 
189         if (href != null)
190             addParameter("href", findString(href));
191         if (errorText != null)
192             addParameter("errorText", findString(errorText));
193         if (loadingText != null)
194             addParameter("loadingText", findString(loadingText));
195         if (afterLoading != null)
196             addParameter("afterLoading", findString(afterLoading));
197         if (beforeLoading != null)
198             addParameter("beforeLoading", findString(beforeLoading));
199         if (executeScripts != null)
200             addParameter("executeScripts", findValue(executeScripts, Boolean.class));
201         if (listenTopics != null)
202             addParameter("listenTopics", findString(listenTopics));
203         if (notifyTopics != null)
204             addParameter("notifyTopics", findString(notifyTopics));
205         if (handler != null)
206             addParameter("handler", findString(handler));
207         if (formId != null)
208             addParameter("formId", findString(formId));
209         if (formFilter != null)
210             addParameter("formFilter", findString(formFilter));
211         if (src != null)
212             addParameter("src", findString(src));
213         if (indicator != null)
214             addParameter("indicator", findString(indicator));
215         if (targets != null)
216             addParameter("targets", findString(targets));
217     }
218 
219     /***
220      * Indicate whether the concrete button supports the type "image".
221      *
222      * @return <tt>true</tt> to indicate type image is supported.
223      */
224     protected boolean supportsImageType() {
225         return true;
226     }
227     
228     /***
229      * Overrides to be able to render body in a template rather than always before the template
230      */
231     public boolean end(Writer writer, String body) {
232         evaluateParams();
233         try {
234             addParameter("body", body);
235             
236             mergeTemplate(writer, buildTemplateName(template, getDefaultTemplate()));
237         } catch (Exception e) {
238             LOG.error("error when rendering", e);
239         }
240         finally {
241             popComponentStack();
242         }
243 
244         return false;
245     }
246 
247     @StrutsTagAttribute(description="Topic that will trigger the remote call")
248     public void setListenTopics(String listenTopics) {
249         this.listenTopics = listenTopics;
250     }
251 
252     @StrutsTagAttribute(description="The theme to use for the element. <b>This tag will usually use the ajax theme.</b>")
253     public void setTheme(String theme) {
254         super.setTheme(theme);
255     }
256 
257     @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.")
258     public void setHref(String href) {
259         this.href = href;
260     }
261 
262     @StrutsTagAttribute(description="The text to display to the user if the is an error fetching the content")
263     public void setErrorText(String errorText) {
264         this.errorText = errorText;
265     }
266 
267     @StrutsTagAttribute(name="onLoadJS", description="Deprecated. Use 'notifyTopics'. Javascript code execute after reload")
268     public void setAfterLoading(String afterLoading) {
269         this.afterLoading = afterLoading;
270     }
271 
272 
273     @StrutsTagAttribute(name="preInvokeJS", description="Deprecated. Use 'notifyTopics'. Javascript code execute before reload")
274     public void setBeforeLoading(String beforeLoading) {
275         this.beforeLoading = beforeLoading;
276     }
277 
278     @StrutsTagAttribute(description="Javascript code in the fetched content will be executed", type="Boolean", defaultValue="false")
279     public void setExecuteScripts(String executeScripts) {
280         this.executeScripts = executeScripts;
281     }
282 
283     @StrutsTagAttribute(description="Text to be shown while content is being fetched", defaultValue="Loading...")
284     public void setLoadingText(String loadingText) {
285         this.loadingText = loadingText;
286     }
287 
288     @StrutsTagAttribute(description="Javascript function name that will make the request")
289     public void setHandler(String handler) {
290         this.handler = handler;
291     }
292 
293     @StrutsTagAttribute(description="Function name used to filter the fields of the form.")
294     public void setFormFilter(String formFilter) {
295         this.formFilter = formFilter;
296     }
297 
298     @StrutsTagAttribute(description="Form id whose fields will be serialized and passed as parameters")
299     public void setFormId(String formId) {
300         this.formId = formId;
301     }
302 
303     @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>.")
304     public void setSrc(String src) {
305         this.src = src;
306     }
307 
308     @StrutsTagAttribute(description="Comma delimited list of ids of the elements whose content will be updated")
309     public void setTargets(String targets) {
310         this.targets = targets;
311     }
312 
313 
314     @StrutsTagAttribute(description="Topics that will published when the remote call completes")
315     public void setNotifyTopics(String notifyTopics) {
316         this.notifyTopics = notifyTopics;
317     }
318 
319 
320     @StrutsTagAttribute(description="Set whether errors will be shown or not", type="Boolean", defaultValue="true")
321     public void setShowErrorTransportText(String showErrorTransportText) {
322         this.showErrorTransportText = showErrorTransportText;
323     }
324 
325 
326     @StrutsTagAttribute(description="Set indicator")
327     public void setIndicator(String indicator) {
328         this.indicator = indicator;
329     }
330 }