View Javadoc

1   /*
2    * $Id: Anchor.java 497654 2007-01-19 00:21:57Z rgielen $
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  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 &lt;a href='' /&gt; 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   * 'notifyTopics' comma separated list of topics names, that will be published. Three parameters are passed:<p/>
75   * <ul>
76   *      <li>data: html or json object when type='load' or type='error'</li>
77   *      <li>type: 'before' before the request is made, 'load' when the request succeeds, or 'error' when it fails</li>
78   *      <li>request: request javascript object, when type='load' or type='error'</li>
79   * </ul>
80   * <!-- END SNIPPET: javadoc -->
81   *
82   * <p/> <b>Examples</b>
83   *
84   * <pre>
85   * <!-- START SNIPPET: example1 -->
86   * &lt;s:a id="link1" theme="ajax" href="/DoIt.action" errorText="An error ocurred" loadingText="Loading..."&gt;
87   *     &lt;img border="none" src="&lt;%=request.getContextPath()%&gt;/images/delete.gif"/&gt;
88   *     &lt;s:param name="id" value="1"/&gt;
89   * &lt;/s:a&gt;
90   * <!-- END SNIPPET: example1 -->
91   * </pre>
92   *
93   * </p>
94   *
95   * <!-- START SNIPPET: exampledescription1 -->
96   *
97   * Results in
98   *
99   * <!-- END SNIPPET: exampledescription1 -->
100  *
101  * </p>
102  *
103  * <pre>
104  * <!-- START SNIPPET: example2 -->
105  * &lt;a dojoType="BindAnchor" executeScripts="true" id="link1" href="/DoIt.action?id=1" errorText="An error ocurred"&gt;&lt;/a&gt;
106  * <!-- END SNIPPET: example2 -->
107  * </pre>
108  *
109  * </p>
110  *
111  * <!-- START SNIPPET: exampledescription2 -->
112  *
113  * Here is an example that uses the beforeLoading. This example is in altSyntax=true:
114  *
115  * <!-- END SNIPPET: exampledescription2 -->
116  *
117  * </p>
118  *
119  * <pre>
120  * <!-- START SNIPPET: example3 -->
121  * &lt;s:a id="test" theme="ajax" href="/simpeResult.action" beforeLoading="confirm('Are you sure?')"&gt;
122  *  A
123  * &lt;/s:a&gt;
124  * <!-- END SNIPPET: example3 -->
125  * </pre>
126  *
127  */
128 @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")
129 public class Anchor extends AbstractRemoteCallUIBean {
130     public static final String OPEN_TEMPLATE = "a";
131     public static final String TEMPLATE = "a-close";
132     public static final String COMPONENT_NAME = Anchor.class.getName();
133 
134     protected String targets;
135 
136     public Anchor(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
137         super(stack, request, response);
138     }
139 
140     public String getDefaultOpenTemplate() {
141         return OPEN_TEMPLATE;
142     }
143 
144     protected String getDefaultTemplate() {
145         return TEMPLATE;
146     }
147 
148     public void evaluateExtraParams() {
149         super.evaluateExtraParams();
150 
151         if(targets != null)
152             addParameter("targets", findString(targets));
153     }
154 
155     @StrutsTagAttribute(description="Comma delimited list of ids of the elements whose content will be updated")
156     public void setTargets(String targets) {
157         this.targets = targets;
158     }
159 
160     @StrutsTagAttribute(name="onLoadJS", description="Deprecated. Use 'notifyTopics'. Javascript code execute after reload")
161     public void setAfterLoading(String afterLoading) {
162         this.afterLoading = afterLoading;
163     }
164 
165 
166     @StrutsTagAttribute(name="preInvokeJS", description="Deprecated. Use 'notifyTopics'. Javascript code execute before reload")
167     public void setBeforeLoading(String beforeLoading) {
168         this.beforeLoading = beforeLoading;
169     }
170 }