View Javadoc

1   /*
2    * $Id: Anchor.java 451544 2006-09-30 05:38:02Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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 &lt;a href='' /&gt; 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   * &lt;s:a id="link1" theme="ajax" href="/DoIt.action" errorText="An error ocurred" showErrorTransportText="true"&gt;
39   *     &lt;img border="none" src="&lt;%=request.getContextPath()%&gt;/images/delete.gif"/&gt;
40   *     &lt;s:param name="id" value="1"/&gt;
41   * &lt;/s:a&gt;
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   * &lt;a dojoType="BindAnchor" evalResult="true" id="link1" href="/DoIt.action?id=1" errorHtml="An error ocurred"
58   * showTransportError="true"&gt;&lt;/a&gt;
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   * &lt;s:a id="test" theme="ajax" href="/simpeResult.action" preInvokeJS="confirm(\'You sure\')"&gt;
75   * 	A
76   * &lt;/s:a&gt;
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 }