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 *
34 * A tag that creates a HTML <a href='' /> that when clicked calls a URL remote XMLHttpRequest call via the dojo
35 * framework.<p/>
36 *
37 * <!-- START SNIPPET: ajaxJavadoc -->
38 * <B>THE FOLLOWING IS ONLY VALID WHEN AJAX IS CONFIGURED</B>
39 * <ul>
40 * <li>href</li>
41 * <li>errorText</li>
42 * <li>listenTopics</li>
43 * <li>notifyTopics</li>
44 * <li>executeScripts</li>
45 * <li>loadingText</li>
46 * <li>listenTopics</li>
47 * <li>handler</li>
48 * <li>formId</li>
49 * <li>formFilter</li>
50 * <li>targets</li>
51 * <li>showErrorTransportText</li>
52 * <li>targets</li>
53 * <li>indicator</li>
54 * </ul>
55 * 'resultDivId' Deprecated. Use targets.<p/>
56 * 'targets' is a list of element ids whose content will be updated with the
57 * text returned from request.<p/>
58 * 'errorText' is the text that will be displayed when there is an error making the request.<p/>
59 * 'onLoadJS' Deprecated. Use 'notifyTopics'.<p/>
60 * 'preInvokeJS' Deprecated. Use 'notifyTopics'.<p/>
61 * 'executeScripts' if set to true will execute javascript sections in the returned text.<p/>
62 * 'loadingText' is the text that will be displayed on the 'targets' elements while making the
63 * request.<p/>
64 * 'handler' is the name of the function that will take care of making the AJAX request. Dojo's widget
65 * and dom node are passed as parameters).<p/>
66 * 'formId' is the id of the html form whose fields will be seralized and passed as parameters
67 * in the request.<p/>
68 * 'formFilter' is the name of a function which will be used to filter the fields that will be
69 * seralized. This function takes as a parameter the element and returns true if the element
70 * should be included.<p/>
71 * 'listenTopics' comma separated list of topics names, that will trigger a request
72 * 'indicator' element to be shown while the request executing
73 * 'showErrorTransportText': whether errors should be displayed (on 'targets')</p>
74 * 'showLoadingText' show loading text on targets</p>
75 * 'notifyTopics' comma separated list of topics names, that will be published. Three parameters are passed:<p/>
76 * <ul>
77 * <li>data: html or json object when type='load' or type='error'</li>
78 * <li>type: 'before' before the request is made, 'load' when the request succeeds, or 'error' when it fails</li>
79 * <li>request: request javascript object, when type='load' or type='error'</li>
80 * </ul>
81 * <!-- END SNIPPET: javadoc -->
82 *
83 * <p/> <b>Examples</b>
84 *
85 * <pre>
86 * <!-- START SNIPPET: example1 -->
87 * <s:a id="link1" theme="ajax" href="/DoIt.action" errorText="An error ocurred" loadingText="Loading...">
88 * <img border="none" src="<%=request.getContextPath()%>/images/delete.gif"/>
89 * <s:param name="id" value="1"/>
90 * </s:a>
91 * <!-- END SNIPPET: example1 -->
92 * </pre>
93 *
94 * </p>
95 *
96 * <!-- START SNIPPET: exampledescription1 -->
97 *
98 * Results in
99 *
100 * <!-- END SNIPPET: exampledescription1 -->
101 *
102 * </p>
103 *
104 * <pre>
105 * <!-- START SNIPPET: example2 -->
106 * <a dojoType="BindAnchor" executeScripts="true" id="link1" href="/DoIt.action?id=1" errorText="An error ocurred"></a>
107 * <!-- END SNIPPET: example2 -->
108 * </pre>
109 *
110 * </p>
111 *
112 * <!-- START SNIPPET: exampledescription2 -->
113 *
114 * Here is an example that uses the beforeLoading. This example is in altSyntax=true:
115 *
116 * <!-- END SNIPPET: exampledescription2 -->
117 *
118 * </p>
119 *
120 * <pre>
121 * <!-- START SNIPPET: example3 -->
122 * <s:a id="test" theme="ajax" href="/simpeResult.action" beforeLoading="confirm('Are you sure?')">
123 * A
124 * </s:a>
125 * <!-- END SNIPPET: example3 -->
126 * </pre>
127 *
128 */
129 @StrutsTag(name="a", tldTagClass="org.apache.struts2.views.jsp.ui.AnchorTag", description="Render a HTML href element that when clicked can optionally call a URL via remote XMLHttpRequest and updates its targets")
130 public class Anchor extends AbstractRemoteCallUIBean {
131 public static final String OPEN_TEMPLATE = "a";
132 public static final String TEMPLATE = "a-close";
133 public static final String COMPONENT_NAME = Anchor.class.getName();
134
135 protected String targets;
136
137 public Anchor(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
138 super(stack, request, response);
139 }
140
141 public String getDefaultOpenTemplate() {
142 return OPEN_TEMPLATE;
143 }
144
145 protected String getDefaultTemplate() {
146 return TEMPLATE;
147 }
148
149 public void evaluateExtraParams() {
150 super.evaluateExtraParams();
151
152 if(targets != null)
153 addParameter("targets", findString(targets));
154 }
155
156 @StrutsTagAttribute(description="Comma delimited list of ids of the elements whose content will be updated")
157 public void setTargets(String targets) {
158 this.targets = targets;
159 }
160
161 @StrutsTagAttribute(name="onLoadJS", description="Deprecated. Use 'notifyTopics'. Javascript code execute after reload")
162 public void setAfterLoading(String afterLoading) {
163 this.afterLoading = afterLoading;
164 }
165
166
167 @StrutsTagAttribute(name="preInvokeJS", description="Deprecated. Use 'notifyTopics'. Javascript code execute before reload")
168 public void setBeforeLoading(String beforeLoading) {
169 this.beforeLoading = beforeLoading;
170 }
171 }