View Javadoc

1   package org.apache.velocity.tools.struts;
2   
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  import javax.servlet.ServletContext;
23  import org.apache.velocity.tools.generic.ValueParser;
24  import org.apache.velocity.tools.view.LinkTool;
25  import org.apache.velocity.tools.view.ViewContext;
26  
27  /**
28   * <p>The StrutsLinkTool extends the standard {@link LinkTool} to add methods
29   * for working with Struts' Actions and Forwards:</p>
30   * <ul>
31   *     <li>translate logical names (Struts forwards, actions ) to URI references, see
32   *       methods {@link #setAction} and {@link #setForward}</li>
33   * </ul>
34   * 
35   * <p><pre>
36   * Template example(s):
37   *   &lt;a href="$link.action.update"&gt;update something&lt;/a&gt;
38   *   #set( $base = $link.forward.MyPage.anchor('view') )
39   *   &lt;a href="$base.param('select','this')"&gt;view this&lt;/a&gt;
40   *   &lt;a href="$base.param('select','that')"&gt;view that&lt;/a&gt;
41   *
42   * Toolbox configuration:
43   * &lt;tools&gt;
44   *   &lt;toolbox scope="request"&gt;
45   *     &lt;tool class="org.apache.velocity.tools.struts.StrutsLinkTool"/&gt;
46   *   &lt;/toolbox&gt;
47   * &lt;/tools&gt;
48   * </pre></p>
49   *
50   * <p>This tool may only be used in the request scope.</p>
51   *
52   * @author <a href="mailto:sidler@teamup.com">Gabe Sidler</a>
53   * @author Nathan Bubna
54   *
55   * @version $Id: StrutsLinkTool.java 707788 2008-10-24 23:28:06Z nbubna $
56   */
57  public class StrutsLinkTool extends LinkTool
58  {
59      protected ServletContext application;
60      private String get;
61  
62      @Override
63      protected void configure(ValueParser props)
64      {
65          super.configure(props);
66  
67          this.application = (ServletContext)props.getValue(ViewContext.SERVLET_CONTEXT_KEY);
68      }
69  
70      /**
71       * <p>This exists to enable a simplified syntax for using this tool in a
72       * template. Now, users can do <code>$link.action.saveFoo</code> instead of
73       * <code>$link.setAction('saveFoo')</code> and
74       * <code>$link.forward.profile</code> instead of
75       * <code>$link.setForward('profile')</code>. Neat, eh? :)</p>
76       * @since VelocityTools 1.3
77       */
78      public StrutsLinkTool get(String getme)
79      {
80          StrutsLinkTool sub = null;
81          if ("action".equalsIgnoreCase(this.get))
82          {
83              sub = setAction(getme);
84          }
85          else if ("forward".equalsIgnoreCase(this.get))
86          {
87              sub = setForward(getme);
88          }
89          else
90          {
91              sub = (StrutsLinkTool)this.duplicate();
92          }
93          if (sub == null)
94          {
95              return null;
96          }
97          sub.get = getme;
98          return sub;
99      }
100 
101 
102     /**
103      * <p>Returns a copy of the link with the given action name
104      * converted into a server-relative URI reference. This method
105      * does not check if the specified action really is defined.
106      * This method will overwrite any previous URI reference settings
107      * but will copy the query string.</p>
108      *
109      * @param action an action path as defined in struts-config.xml
110      *
111      * @return a new instance of StrutsLinkTool
112      */
113     public StrutsLinkTool setAction(String action)
114     {
115         String url =
116             StrutsUtils.getActionMappingURL(application, request, action);
117         if (url == null)
118         {
119             debug("StrutsLinkTool : In method setAction("+action+
120                   "): Parameter does not map to a valid action.");
121             return null;
122         }
123         return (StrutsLinkTool)absolute(url);
124     }
125 
126 
127     /**
128      * <p>Returns a copy of the link with the given global or local forward
129      * name converted into a server-relative URI reference. If the parameter
130      * does not map to an existing global forward name, <code>null</code>
131      * is returned. This method will overwrite any previous URI reference
132      * settings but will copy the query string.</p>
133      *
134      * @param forward a forward name as defined in struts-config.xml
135      *                in either global-forwards or in the currently executing
136      *                action mapping.
137      *
138      * @return a new instance of StrutsLinkTool
139      */
140     public StrutsLinkTool setForward(String forward)
141     {
142         String url = StrutsUtils.getForwardURL(request, application, forward);
143         if (url == null)
144         {
145             debug("StrutsLinkTool : In method setForward(" + forward +
146                   "): Parameter does not map to a valid forward.");
147             return null;
148         }
149         return (StrutsLinkTool)absolute(url);
150     }
151 
152 }