1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts2.dojo.components;
23
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
26
27 import org.apache.struts2.views.annotations.StrutsTag;
28 import org.apache.struts2.views.annotations.StrutsTagAttribute;
29 import org.apache.struts2.views.annotations.StrutsTagSkipInheritance;
30
31 import com.opensymphony.xwork2.util.ValueStack;
32
33 /***
34 * <!-- START SNIPPET: javadoc -->
35 * <p>
36 * A tag that creates an HTML <a/> element, that when clicked makes an asynchronous request(XMLHttpRequest). The url
37 * attribute must be build using the <s:url/> tag.
38 * </p>
39 * <!-- END SNIPPET: javadoc -->
40 * <p>Examples</p>
41 *
42 * <!-- START SNIPPET: example1 -->
43 * <div id="div1">Div 1</div>
44 * <s:url id="ajaxTest" value="/AjaxTest.action"/>
45 *
46 * <sx:a id="link1" href="%{ajaxTest}" target="div1">
47 * Update Content
48 * </sx:a>
49 * <!-- END SNIPPET: example1 -->
50 *
51 * <!-- START SNIPPET: example2 -->
52 * <s:form id="form" action="AjaxTest">
53 * <input type="textbox" name="data">
54 * <sx:a>Submit form</sx:a>
55 * </s:form>
56 * <!-- END SNIPPET: example2 -->
57 *
58 * <!-- START SNIPPET: example3 -->
59 * <s:form id="form" action="AjaxTest">
60 * <input type="textbox" name="data">
61 * </s:form>
62 *
63 * <sx:a formId="form">Submit form</sx:a>
64 * <!-- END SNIPPET: example3 -->
65 *
66 * <!-- START SNIPPET: example4 -->
67 * <script type="text/javascript">
68 * dojo.event.topic.subscribe("/before", function(event, widget){
69 * alert('inside a topic event. before request');
70 * //event: set event.cancel = true, to cancel request
71 * //widget: widget that published the topic
72 * });
73 * </script>
74 *
75 * <sx:a beforeNotifyTopics="/before">Publish topics</sx:a>
76 * <!-- END SNIPPET: example4 -->
77 *
78 * <!-- START SNIPPET: example5 -->
79 * <script type="text/javascript">
80 * dojo.event.topic.subscribe("/after", function(data, request, widget){
81 * alert('inside a topic event. after request');
82 * //data : text returned from request(the html)
83 * //request: XMLHttpRequest object
84 * //widget: widget that published the topic
85 * });
86 * </script>
87 *
88 * <sx:a afterNotifyTopics="/after" highlightColor="red" href="%{#ajaxTest}">Publish topics</sx:a>
89 * <!-- END SNIPPET: example5 -->
90 *
91 * <!-- START SNIPPET: example6 -->
92 * <script type="text/javascript">
93 * dojo.event.topic.subscribe("/error", function(error, request, widget){
94 * alert('inside a topic event. on error');
95 * //error : error object (error.message has the error message)
96 * //request: XMLHttpRequest object
97 * //widget: widget that published the topic
98 * });
99 * </script>
100 *
101 * <img id="ind1" src="${pageContext.request.contextPath}/images/indicator.gif" style="display:none"/>
102 * <sx:a errorNotifyTopics="/error" indicator="ind1" href="%{#ajaxTest}">Publish topics</sx:a>
103 * <!-- END SNIPPET: example6 -->
104 */
105 @StrutsTag(name="a", tldTagClass="org.apache.struts2.dojo.views.jsp.ui.AnchorTag", description="Renders an HTML anchor element that when clicked calls a URL via remote XMLHttpRequest and updates " +
106 "its targets content")
107 public class Anchor extends AbstractValidateBean {
108 public static final String OPEN_TEMPLATE = "a";
109 public static final String TEMPLATE = "a-close";
110 public static final String COMPONENT_NAME = Anchor.class.getName();
111
112 protected String targets;
113
114 public Anchor(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
115 super(stack, request, response);
116 }
117
118 public String getDefaultOpenTemplate() {
119 return OPEN_TEMPLATE;
120 }
121
122 protected String getDefaultTemplate() {
123 return TEMPLATE;
124 }
125
126 public void evaluateExtraParams() {
127 super.evaluateExtraParams();
128
129 if (targets != null)
130 addParameter("targets", findString(targets));
131 }
132
133 @Override
134 @StrutsTagSkipInheritance
135 public void setTheme(String theme) {
136 super.setTheme(theme);
137 }
138
139 @StrutsTagAttribute(description="Comma delimited list of ids of the elements whose content will be updated")
140 public void setTargets(String targets) {
141 this.targets = targets;
142 }
143 }