1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 <DIV /> 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.<p/>
73 * 'autoStart' if set to true(true by default) starts the timer automatically<p/>
74 * 'startTimerListenTopics' is a comma-separated list of topics used to start the timer<p/>
75 * 'stopTimerListenTopics' is a comma-separated list of topics used to stop the timer<p/>
76 * 'listenTopics' comma separated list of topics names, that will trigger a request<p/>
77 * 'indicator' element to be shown while the request executing<p/>
78 * 'showErrorTransportText': whether errors should be displayed (on 'targets')</p>
79 * 'showLoadingText' show loading text on targets</p>
80 * 'separateScript' Run scripts in a separate scope, unique for each Div<p/>
81 * 'notifyTopics' comma separated list of topics names, that will be published. Three parameters are passed:<p/>
82 * <ul>
83 * <li>data: html or json object when type='load' or type='error'</li>
84 * <li>type: 'before' before the request is made, 'load' when the request succeeds, or 'error' when it fails</li>
85 * <li>request: request javascript object, when type='load' or type='error'</li>
86 * </ul>
87 * <!-- END SNIPPET: javadoc -->
88 * </div><p> <b>Examples</b>
89 *
90 * <pre>
91 * <!-- START SNIPPET: example -->
92 * <s:url id="url" action="AjaxTest" />
93 * <s:div
94 * id="once"
95 * theme="ajax"
96 * href="%{url}"
97 * loadingText="Loading..."
98 * listenTopics="/refresh"
99 * updateFreq="3000"
100 * autoStart="true"
101 * formId="form"
102 *></s:div>
103 * <!-- END SNIPPET: example -->
104 * </pre>
105 * </p>
106 *
107 */
108 @StrutsTag(name="div", tldTagClass="org.apache.struts2.views.jsp.ui.DivTag", description="Render HTML div providing content from remote call via AJAX")
109 public class Div extends AbstractRemoteCallUIBean {
110
111 public static final String TEMPLATE = "div";
112 public static final String TEMPLATE_CLOSE = "div-close";
113 public static final String COMPONENT_NAME = Div.class.getName();
114
115 protected String updateFreq;
116 protected String autoStart;
117 protected String delay;
118 protected String startTimerListenTopics;
119 protected String stopTimerListenTopics;
120 protected String refreshOnShow;
121 protected String separateScripts;
122
123 public Div(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
124 super(stack, request, response);
125 }
126
127 public String getDefaultOpenTemplate() {
128 return TEMPLATE;
129 }
130
131 protected String getDefaultTemplate() {
132 return TEMPLATE_CLOSE;
133 }
134
135 public void evaluateExtraParams() {
136 super.evaluateExtraParams();
137
138 if (updateFreq != null)
139 addParameter("updateFreq", findValue(updateFreq, Integer.class));
140 if (autoStart != null)
141 addParameter("autoStart", findValue(autoStart, Boolean.class));
142 if (refreshOnShow != null)
143 addParameter("refreshOnShow", findValue(refreshOnShow, Boolean.class));
144 if (delay != null)
145 addParameter("delay", findValue(delay, Integer.class));
146 if (startTimerListenTopics != null)
147 addParameter("startTimerListenTopics", findString(startTimerListenTopics));
148 if (stopTimerListenTopics != null)
149 addParameter("stopTimerListenTopics", findString(stopTimerListenTopics));
150 if (separateScripts != null)
151 addParameter("separateScripts", findValue(separateScripts, Boolean.class));
152 }
153
154 @StrutsTagAttribute(description="Start timer automatically", type="Boolean", defaultValue="true")
155 public void setAutoStart(String autoStart) {
156 this.autoStart = autoStart;
157 }
158
159 @StrutsTagAttribute(description="How long to wait before fetching the content (in milliseconds)", type="Integer")
160 public void setDelay(String delay) {
161 this.delay = delay;
162 }
163
164 @StrutsTagAttribute(description="How often to reload the content (in milliseconds)", type="Integer")
165 public void setUpdateFreq(String updateInterval) {
166 this.updateFreq = updateInterval;
167 }
168
169 @StrutsTagAttribute(description="Topics that will start the timer (for autoupdate)")
170 public void setStartTimerListenTopics(String startTimerListenTopic) {
171 this.startTimerListenTopics = startTimerListenTopic;
172 }
173
174 @StrutsTagAttribute(description="Topics that will stop the timer (for autoupdate)")
175 public void setStopTimerListenTopics(String stopTimerListenTopic) {
176 this.stopTimerListenTopics = stopTimerListenTopic;
177 }
178
179 @StrutsTagAttribute(description="Content will be loaded when div becomes visible, used only inside tabbedPanel", type="Boolean", defaultValue="false")
180 public void setRefreshOnShow(String refreshOnShow) {
181 this.refreshOnShow = refreshOnShow;
182 }
183
184 @StrutsTagAttribute(description="Deprecated. Use 'notifyTopics'. Javascript code execute after reload")
185 public void setAfterLoading(String afterLoading) {
186 this.afterLoading = afterLoading;
187 }
188
189 @StrutsTagAttribute(description="Run scripts in a separate scope, unique for each Div", defaultValue="true")
190 public void setSeparateScripts(String separateScripts) {
191 this.separateScripts = separateScripts;
192 }
193 }