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 com.opensymphony.xwork2.util.ValueStack;
24
25 /***
26 * <!-- START SNIPPET: javadoc -->
27 *
28 * A tag that creates a HTML <a href='' /> that when clicked calls a URL remote XMLHttpRequest call via the dojo
29 * framework. The result from the URL is executed as JavaScript. If a "listenTopics" is supplied, it will publish a
30 * 'click' message to that topic when the result is returned.
31 *
32 * <!-- END SNIPPET: javadoc -->
33 *
34 * <p/> <b>Examples</b>
35 *
36 * <pre>
37 * <!-- START SNIPPET: example1 -->
38 * <s:a id="link1" theme="ajax" href="/DoIt.action" errorText="An error ocurred" showErrorTransportText="true">
39 * <img border="none" src="<%=request.getContextPath()%>/images/delete.gif"/>
40 * <s:param name="id" value="1"/>
41 * </s:a>
42 * <!-- END SNIPPET: example1 -->
43 * </pre>
44 *
45 * </p>
46 *
47 * <!-- START SNIPPET: exampledescription1 -->
48 *
49 * Results in
50 *
51 * <!-- END SNIPPET: exampledescription1 -->
52 *
53 * </p>
54 *
55 * <pre>
56 * <!-- START SNIPPET: example2 -->
57 * <a dojoType="BindAnchor" evalResult="true" id="link1" href="/DoIt.action?id=1" errorHtml="An error ocurred"
58 * showTransportError="true"></a>
59 * <!-- END SNIPPET: example2 -->
60 * </pre>
61 *
62 * </p>
63 *
64 * <!-- START SNIPPET: exampledescription2 -->
65 *
66 * Here is an example that uses the postInvokeJS. This example is in altSyntax=true:
67 *
68 * <!-- END SNIPPET: exampledescription2 -->
69 *
70 * </p>
71 *
72 * <pre>
73 * <!-- START SNIPPET: example3 -->
74 * <s:a id="test" theme="ajax" href="/simpeResult.action" preInvokeJS="confirm(\'You sure\')">
75 * A
76 * </s:a>
77 * <!-- END SNIPPET: example3 -->
78 * </pre>
79 *
80 * @s.tag name="a" tld-body-content="JSP" tld-tag-class="org.apache.struts2.views.jsp.ui.AnchorTag"
81 * description="Render a HTML href element that when clicked calls a URL via remote XMLHttpRequest"
82 *
83 */
84 public class Anchor extends RemoteCallUIBean {
85 final public static String OPEN_TEMPLATE = "a";
86 final public static String TEMPLATE = "a-close";
87 final public static String COMPONENT_NAME = Anchor.class.getName();
88
89 protected String notifyTopics;
90 protected String preInvokeJS;
91
92 public Anchor(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
93 super(stack, request, response);
94 }
95
96 public String getDefaultOpenTemplate() {
97 return OPEN_TEMPLATE;
98 }
99
100 protected String getDefaultTemplate() {
101 return TEMPLATE;
102 }
103
104 public void evaluateExtraParams() {
105 super.evaluateExtraParams();
106
107 if (notifyTopics != null) {
108 addParameter("notifyTopics", findString(notifyTopics));
109 }
110
111 if (preInvokeJS != null) {
112 addParameter("preInvokeJS", findString(preInvokeJS));
113 }
114 }
115
116 /***
117 * The id to assign the component
118 * @s.tagattribute required="false" type="String"
119 */
120 public void setId(String id) {
121 super.setId(id);
122 }
123
124 /***
125 * Topic names to post an event to after the remote call has been made
126 * @s.tagattribute required="false"
127 */
128 public void setNotifyTopics(String notifyTopics) {
129 this.notifyTopics = notifyTopics;
130 }
131
132 /***
133 * A javascript snippet that will be invoked prior to the execution of the target href. If provided must return true or false. True indicates to continue executing target, false says do not execute link target. Possible uses are for confirm dialogs.
134 * @s.tagattribute required="false" type="String"
135 */
136 public void setPreInvokeJS(String preInvokeJS) {
137 this.preInvokeJS = preInvokeJS;
138 }
139 }