KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > velocity > tools > struts > StrutsLinkTool


1 /*
2  * Copyright 2003 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.velocity.tools.struts;
18
19 import org.apache.velocity.app.Velocity;
20 import org.apache.velocity.tools.view.tools.LinkTool;
21 import org.apache.velocity.tools.struts.StrutsUtils;
22
23 /**
24  * <p>View tool to work with URI links in Struts.</p>
25  * <p><pre>
26  * Template example(s):
27  * &lt;a HREF="$link.setAction('update')"&gt;update something&lt;/a&gt;
28  * #set( $base = $link.setForward('MyPage.vm').setAnchor('view') )
29  * &lt;a HREF="$base.addQueryData('select','this')"&gt;view this&lt;/a&gt;
30  * &lt;a HREF="$base.addQueryData('select','that')"&gt;view that&lt;/a&gt;
31  *
32  * Toolbox configuration:
33  * &lt;tool&gt;
34  * &lt;key&gt;link&lt;/key&gt;
35  * &lt;scope&gt;request&lt;/scope&gt;
36  * &lt;class&gt;org.apache.velocity.tools.struts.StrutsLinkTool&lt;/class&gt;
37  * &lt;/tool&gt;
38  * </pre></p>
39  *
40  * <p>This tool should only be used in the request scope.</p>
41  *
42  * @author <a HREF="mailto:sidler@teamup.com">Gabe Sidler</a>
43  * @author <a HREF="mailto:nathan@esha.com">Nathan Bubna</a>
44  *
45  * @version $Id: StrutsLinkTool.java,v 1.7 2004/02/18 20:09:51 nbubna Exp $
46  */

47 public class StrutsLinkTool extends LinkTool
48 {
49
50
51     /**
52      * <p>Returns a copy of the link with the given action name
53      * converted into a server-relative URI reference. This method
54      * does not check if the specified action really is defined.
55      * This method will overwrite any previous URI reference settings
56      * but will copy the query string.</p>
57      *
58      * @param action an action path as defined in struts-config.xml
59      *
60      * @return a new instance of StrutsLinkTool
61      */

62     public StrutsLinkTool setAction(String JavaDoc action)
63     {
64         return (StrutsLinkTool)copyWith(
65             StrutsUtils.getActionMappingURL(application, request, action));
66     }
67     
68     
69     /**
70      * <p>Returns a copy of the link with the given global forward name
71      * converted into a server-relative URI reference. If the parameter
72      * does not map to an existing global forward name, <code>null</code>
73      * is returned. This method will overwrite any previous URI reference
74      * settings but will copy the query string.</p>
75      *
76      * @param forward a global forward name as defined in struts-config.xml
77      *
78      * @return a new instance of StrutsLinkTool
79      */

80     public StrutsLinkTool setForward(String JavaDoc forward)
81     {
82         String JavaDoc url = StrutsUtils.getForwardURL(request, application, forward);
83         if (url == null)
84         {
85             Velocity.warn("StrutsLinkTool: In method setForward(" + forward +
86                           "): Parameter does not map to a valid forward.");
87             return null;
88         }
89         return (StrutsLinkTool)copyWith(url);
90     }
91
92
93 }
94
Popular Tags