1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.struts2.components;
19
20 import javax.servlet.http.HttpServletRequest;
21 import javax.servlet.http.HttpServletResponse;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.struts2.views.util.UrlHelper;
26
27 import com.opensymphony.xwork2.util.ValueStack;
28
29 /***
30 * <!-- START SNIPPET: javadoc -->
31 * The div tag is primarily an AJAX tag, providing a remote call from the current page to update a section
32 * of content without having to refresh the entire page.<p/>
33 *
34 * It creates a HTML <DIV /> that obtains it's content via a remote XMLHttpRequest call
35 * via the dojo framework.<p/>
36 *
37 * If a "listenTopics" is supplied, it will listen to that topic and refresh it's content when any message
38 * is received.<p/>
39 * <!-- END SNIPPET: javadoc -->
40 *
41 * <b>Important:</b> Be sure to setup the page containing this tag to be Configured for AJAX</p>
42 *
43 * <p/> <b>Examples</b>
44 *
45 * <pre>
46 * <!-- START SNIPPET: example -->
47 * <s:div ... />
48 * <!-- END SNIPPET: example -->
49 * </pre>
50 *
51 * @s.tag name="div" tld-body-content="JSP" tld-tag-class="org.apache.struts2.views.jsp.ui.DivTag"
52 * description="Render HTML div providing content from remote call via AJAX"
53 */
54 public class Div extends RemoteCallUIBean {
55
56 private static final Log _log = LogFactory.getLog(Div.class);
57
58
59 public static final String TEMPLATE = "div";
60 public static final String TEMPLATE_CLOSE = "div-close";
61 public static final String COMPONENT_NAME = Div.class.getName();
62
63 protected String updateFreq;
64 protected String delay;
65 protected String loadingText;
66 protected String listenTopics;
67
68 public Div(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
69 super(stack, request, response);
70 }
71
72 public String getDefaultOpenTemplate() {
73 return TEMPLATE;
74 }
75
76 protected String getDefaultTemplate() {
77 return TEMPLATE_CLOSE;
78 }
79
80 public void evaluateExtraParams() {
81 super.evaluateExtraParams();
82
83 if (null != updateFreq && !"".equals(updateFreq)) {
84 addParameter("updateFreq", findString(updateFreq));
85 } else {
86 addParameter("updateFreq", "0");
87 }
88
89 if (null != delay && !"".equals(delay)) {
90 addParameter("delay", findString(delay));
91 } else {
92 addParameter("delay", "0");
93 }
94
95 String tmpUpdateFreq = (String) getParameters().get("delay");
96 String tmpDelay = (String) getParameters().get("updateFreq");
97 try {
98 int _updateFreq = Integer.parseInt(tmpUpdateFreq);
99 int _delay = Integer.parseInt(tmpDelay);
100
101 if (_updateFreq <= 0 && _delay <= 0) {
102 addParameter("autoStart", "false");
103 }
104 }
105 catch(NumberFormatException e) {
106
107
108 _log.info("error while parsing updateFreq ["+tmpUpdateFreq+"] or delay ["+tmpDelay+"] to integer, cannot determine autoStart mode", e);
109 }
110
111 if (loadingText != null) {
112 addParameter("loadingText", findString(loadingText));
113 }
114
115 if (listenTopics != null) {
116 addParameter("listenTopics", findString(listenTopics));
117 }
118
119 if (href != null) {
120
121
122 addParameter("href", null);
123 addParameter("href", UrlHelper.buildUrl(findString(href), request, response, null));
124 }
125 }
126
127 /***
128 * How often to re-fetch the content (in milliseconds)
129 * @s.tagattribute required="false" type="Integer" default="0"
130 */
131 public void setUpdateFreq(String updateFreq) {
132 this.updateFreq = updateFreq;
133 }
134
135 /***
136 * How long to wait before fetching the content (in milliseconds)
137 * @s.tagattribute required="false" type="Integer" default="0"
138 */
139 public void setDelay(String delay) {
140 this.delay = delay;
141 }
142
143 /***
144 * The text to display to the user while the new content is being fetched (especially good if the content will take awhile)
145 * @s.tagattribute required="false" rtexprvalue="true"
146 */
147 public void setLoadingText(String loadingText) {
148 this.loadingText = loadingText;
149 }
150
151 /***
152 * Topic name to listen to (comma delimited), that will cause the DIV's content to be re-fetched
153 * @s.tagattribute required="false"
154 */
155 public void setListenTopics(String listenTopics) {
156 this.listenTopics = listenTopics;
157 }
158
159 }