View Javadoc

1   /*
2    * $Id: Anchor.java 651946 2008-04-27 13:41:38Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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 &lt;a/&gt; element, that when clicked makes an asynchronous request(XMLHttpRequest). The url
37   * attribute must be build using the &lt;s:url/&gt; tag. 
38   * </p>
39   * <!-- END SNIPPET: javadoc -->
40   * <p>Examples</p>
41   * 
42   * <!-- START SNIPPET: example1 -->
43   * &lt;div id="div1"&gt;Div 1&lt;/div&gt;
44   * &lt;s:url id="ajaxTest" value="/AjaxTest.action"/&gt;
45   * 
46   * &lt;sx:a id="link1" href="%{ajaxTest}" target="div1"&gt;
47   *      Update Content
48   * &lt;/sx:a&gt;
49   * <!-- END SNIPPET: example1 -->
50   * 
51   * <!-- START SNIPPET: example2 -->
52   * &lt;s:form id="form" action="AjaxTest"&gt;
53   *      &lt;input type="textbox" name="data"&gt;
54   *      &lt;sx:a&gt;Submit form&lt;/sx:a&gt;           
55   * &lt;/s:form&gt;
56   * <!-- END SNIPPET: example2 -->
57   * 
58   * <!-- START SNIPPET: example3 -->
59   * &lt;s:form id="form" action="AjaxTest"&gt;
60   *      &lt;input type="textbox" name="data"&gt;   
61   * &lt;/s:form&gt;
62   * 
63   * &lt;sx:a formId="form"&gt;Submit form&lt;/sx:a&gt;
64   * <!-- END SNIPPET: example3 -->
65   * 
66   * <!-- START SNIPPET: example4 -->
67   * &lt;script type="text/javascript"&gt;
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   * &lt;/script&gt;         
74   * 
75   * &lt;sx:a beforeNotifyTopics="/before"&gt;Publish topics&lt;/sx:a&gt;
76   * <!-- END SNIPPET: example4 -->
77   * 
78   * <!-- START SNIPPET: example5 -->
79   * &lt;script type="text/javascript"&gt;
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   * &lt;/script&gt;        
87   * 
88   * &lt;sx:a afterNotifyTopics="/after" highlightColor="red" href="%{#ajaxTest}"&gt;Publish topics&lt;/sx:a&gt;
89   * <!-- END SNIPPET: example5 -->
90   * 
91   * <!-- START SNIPPET: example6 -->
92   * &lt;script type="text/javascript"&gt;
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   * &lt;/script&gt;         
100  * 
101  * &lt;img id="ind1" src="${pageContext.request.contextPath}/images/indicator.gif" style="display:none"/&gt;
102  * &lt;sx:a errorNotifyTopics="/error" indicator="ind1" href="%{#ajaxTest}"&gt;Publish topics&lt;/sx:a&gt;
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 }