View Javadoc

1   /*
2    * $Id: Div.java 497654 2007-01-19 00:21:57Z rgielen $
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 javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
25  
26  import org.apache.struts2.views.annotations.StrutsTag;
27  import org.apache.struts2.views.annotations.StrutsTagAttribute;
28  
29  import com.opensymphony.xwork2.util.ValueStack;
30  
31  /***
32   * <!-- START SNIPPET: javadoc -->
33   * The div tag when used on the ajax theme, provides a remote call
34   * from the current page to update a section of content without having to refresh the entire page.
35   * <p>
36   * It creates a HTML &lt;DIV /&gt; that obtains it's content via a remote XMLHttpRequest call via
37   * the dojo framework.
38   * </p>
39   * <div>
40   * <!-- START SNIPPET: ajaxJavadoc -->
41   * <B>THE FOLLOWING IS ONLY VALID WHEN AJAX IS CONFIGURED</B>
42   * <ul>
43   *      <li>href</li>
44   *      <li>errorText</li>
45   *      <li>afterLoading</li>
46   *      <li>executeScripts</li>
47   *      <li>loadingText</li>
48   *      <li>listenTopics</li>
49   *      <li>handler</li>
50   *      <li>formId</li>
51   *      <li>formFilter</li>
52   *      <li>targets</li>
53   *      <li>notifyTopics</li>
54   *      <li>showErrorTransportText</li>
55   *      <li>indicator</li>
56   * </ul>
57   * 'targets' is a list of element ids whose content will be updated with the
58   * text returned from request.<p/>
59   * 'href' needs to be set as an url tag reference value.<p/>
60   * 'errorText' is the text that will be displayed when there is an error making the request.<p/>
61   * 'afterLoading' Deprecated. Use 'notifyTopics'.<p/>
62   * 'executeScripts' if set to true will execute javascript sections in the returned text.<p/>
63   * 'loadingText' is the text that will be displayed on the 'targets' elements while making the
64   * request.<p/>
65   * 'handler' is the name of the function that will take care of making the AJAX request. Dojo's widget
66   * and dom node are passed as parameters).<p/>
67   * 'formId' is the id of the html form whose fields will be seralized and passed as parameters
68   * in the request.<p/>
69   * 'formFilter' is the name of a function which will be used to filter the fields that will be
70   * seralized. This function takes as a parameter the element and returns true if the element
71   * should be included.<p/>
72   * 'updateFreq' sets(in milliseconds) the update interval.
73   * 'autoStart' if set to true(true by default) starts the timer automatically
74   * 'startTimerListenTopics' is a comma-separated list of topics used to start the timer
75   * 'stopTimerListenTopics' is a comma-separated list of topics used to stop the timer
76   * 'listenTopics' comma separated list of topics names, that will trigger a request
77   * 'indicator' element to be shown while the request executing
78   * 'showErrorTransportText': whether errors should be displayed (on 'targets')</p>
79   * 'notifyTopics' comma separated list of topics names, that will be published. Three parameters are passed:
80   * <ul>
81   *      <li>data: html or json object when type='load' or type='error'</li>
82   *      <li>type: 'before' before the request is made, 'load' when the request succeeds, or 'error' when it fails</li>
83   *      <li>request: request javascript object, when type='load' or type='error'</li>
84   * </ul>
85   * <!-- END SNIPPET: javadoc -->
86   * </div><p> <b>Examples</b>
87   *
88   * <pre>
89   *       <!-- START SNIPPET: example -->
90   * &lt;s:url id="url" action="AjaxTest" />
91   * &lt;s:div
92   *    id=&quot;once&quot;
93   *    theme=&quot;ajax&quot;
94   *    href=&quot;%{url}&quot;
95   *    loadingText=&quot;Loading...&quot;
96   *    listenTopics=&quot;/refresh&quot;
97   *    updateFreq=&quot;3000&quot;
98   *    autoStart=&quot;true&quot;
99   *    formId=&quot;form&quot;
100  *&gt;&lt;/s:div&gt;
101  *       <!-- END SNIPPET: example -->
102  * </pre>
103  * </p>
104  *
105  */
106 @StrutsTag(name="div", tldTagClass="org.apache.struts2.views.jsp.ui.DivTag", description="Render HTML div providing content from remote call via AJAX")
107 public class Div extends AbstractRemoteCallUIBean {
108 
109     public static final String TEMPLATE = "div";
110     public static final String TEMPLATE_CLOSE = "div-close";
111     public static final String COMPONENT_NAME = Div.class.getName();
112 
113     protected String updateFreq;
114     protected String autoStart;
115     protected String delay;
116     protected String startTimerListenTopics;
117     protected String stopTimerListenTopics;
118     protected String refreshOnShow;
119 
120     public Div(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
121         super(stack, request, response);
122     }
123 
124     public String getDefaultOpenTemplate() {
125         return TEMPLATE;
126     }
127 
128     protected String getDefaultTemplate() {
129         return TEMPLATE_CLOSE;
130     }
131 
132     public void evaluateExtraParams() {
133         super.evaluateExtraParams();
134 
135         if (updateFreq != null)
136             addParameter("updateFreq", findValue(updateFreq, Integer.class));
137         if (autoStart != null)
138             addParameter("autoStart", findValue(autoStart, Boolean.class));
139         if (refreshOnShow != null)
140             addParameter("refreshOnShow", findValue(refreshOnShow, Boolean.class));
141         if (delay != null)
142             addParameter("delay", findValue(delay, Integer.class));
143         if (startTimerListenTopics != null)
144             addParameter("startTimerListenTopics", findString(startTimerListenTopics));
145         if (stopTimerListenTopics != null)
146             addParameter("stopTimerListenTopics", findString(stopTimerListenTopics));
147     }
148 
149     @StrutsTagAttribute(description="Start timer automatically", type="Boolean", defaultValue="true")
150     public void setAutoStart(String autoStart) {
151         this.autoStart = autoStart;
152     }
153 
154     @StrutsTagAttribute(description="How long to wait before fetching the content (in milliseconds)",  type="Integer")
155     public void setDelay(String delay) {
156         this.delay = delay;
157     }
158 
159     @StrutsTagAttribute(description="How often to reload the content (in milliseconds)", type="Integer")
160     public void setUpdateFreq(String updateInterval) {
161         this.updateFreq = updateInterval;
162     }
163 
164     @StrutsTagAttribute(description="Topics that will start the timer (for autoupdate)")
165     public void setStartTimerListenTopics(String startTimerListenTopic) {
166         this.startTimerListenTopics = startTimerListenTopic;
167     }
168 
169     @StrutsTagAttribute(description="Topics that will stop the timer (for autoupdate)")
170     public void setStopTimerListenTopics(String stopTimerListenTopic) {
171         this.stopTimerListenTopics = stopTimerListenTopic;
172     }
173 
174     @StrutsTagAttribute(description="Content will be loaded when div becomes visible, used only inside tabbedPanel", type="Boolean", defaultValue="false")
175     public void setRefreshOnShow(String refreshOnShow) {
176         this.refreshOnShow = refreshOnShow;
177     }
178 
179     @StrutsTagAttribute(description="Deprecated. Use 'notifyTopics'. Javascript code execute after reload")
180     public void setAfterLoading(String afterLoading) {
181         this.afterLoading = afterLoading;
182     }
183 }