KickJava   Java API By Example, From Geeks To Geeks.

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


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
18 package org.apache.velocity.tools.struts;
19
20 import javax.servlet.http.HttpServletRequest JavaDoc;
21 import javax.servlet.http.HttpSession JavaDoc;
22 import javax.servlet.ServletContext JavaDoc;
23
24 import org.apache.struts.util.MessageResources;
25 import org.apache.struts.action.*;
26
27 import org.apache.velocity.tools.view.context.ViewContext;
28 import org.apache.velocity.tools.view.tools.ViewTool;
29
30
31 /**
32  * <p>View tool to work with HTML forms in Struts.</p>
33  * <p><pre>
34  * Template example(s):
35  * &lt;input type="hidden" name="$form.tokenName" value="$form.token"&gt;
36  * &lt;input type="submit" name="$form.cancelName" value="Cancel"&gt;
37  *
38  * Toolbox configuration:
39  *
40  * &lt;tool&gt;
41  * &lt;key&gt;form&lt;/key&gt;
42  * &lt;scope&gt;request&lt;/scope&gt;
43  * &lt;class&gt;org.apache.velocity.tools.struts.FormTool&lt;/class&gt;
44  * &lt;/tool&gt;
45  * </pre></p>
46  *
47  * <p>This tool should only be used in the request scope.</p>
48  *
49  * @author <a HREF="mailto:sidler@teamup.com">Gabe Sidler</a>
50  * @since VelocityTools 1.0
51  * @version $Id: FormTool.java,v 1.6 2004/02/18 20:09:51 nbubna Exp $
52  */

53 public class FormTool implements ViewTool
54 {
55
56     // --------------------------------------------- Properties ---------------
57

58     
59     /**
60      * A reference to the HtttpServletRequest.
61      */

62     protected HttpServletRequest JavaDoc request;
63     
64
65     /**
66      * A reference to the HtttpSession.
67      */

68     protected HttpSession JavaDoc session;
69
70
71     
72     // --------------------------------------------- Constructors -------------
73

74     /**
75      * Default constructor. Tool must be initialized before use.
76      */

77     public FormTool()
78     {
79     }
80     
81     
82     /**
83      * Initializes this tool.
84      *
85      * @param obj the current ViewContext
86      * @throws IllegalArgumentException if the param is not a ViewContext
87      */

88     public void init(Object JavaDoc obj)
89     {
90         if (!(obj instanceof ViewContext))
91         {
92             throw new IllegalArgumentException JavaDoc("Tool can only be initialized with a ViewContext");
93         }
94
95         ViewContext context = (ViewContext)obj;
96         this.request = context.getRequest();
97         this.session = request.getSession(false);
98     }
99
100
101
102     // --------------------------------------------- View Helpers -------------
103

104     /**
105      * <p>Returns the form bean associated with this action mapping.</p>
106      *
107      * <p>This is a convenience method. The form bean is automatically
108      * available in the Velocity context under the name defined in the
109      * Struts configuration.</p>
110      *
111      * <p>If the form bean is used repeatedly, it is recommended to create a
112      * local variable referencing the bean rather than calling getBean()
113      * multiple times.</p>
114      *
115      * <pre>
116      * Example:
117      * #set ($defaults = $form.bean)
118      * &lt;input type="text" name="username" value="$defaults.username"&gt;
119      * </pre>
120      *
121      * @return the {@link ActionForm} associated with this request or
122      * <code>null</code> if there is no form bean associated with this mapping
123      */

124     public ActionForm getBean()
125     {
126         return StrutsUtils.getActionForm(request, session);
127     }
128     
129
130     /**
131      * <p>Returns the query parameter name under which a cancel button press
132      * must be reported if form validation is to be skipped.</p>
133      *
134      * <p>This is the value of
135      * <code>org.apache.struts.taglib.html.Constants.CANCEL_PROPERTY</code></p>
136      */

137     public String JavaDoc getCancelName()
138     {
139         return org.apache.struts.taglib.html.Constants.CANCEL_PROPERTY;
140     }
141     
142
143     /**
144      * Returns the transaction control token for this session or
145      * <code>null</code> if no token exists.
146      */

147     public String JavaDoc getToken()
148     {
149         return StrutsUtils.getToken(session);
150     }
151
152
153     /**
154      * <p>Returns the query parameter name under which a transaction token
155      * must be reported. This is the value of
156      * <code>org.apache.struts.taglib.html.Constants.TOKEN_KEY</code></p>
157      */

158     public String JavaDoc getTokenName()
159     {
160         return org.apache.struts.taglib.html.Constants.TOKEN_KEY;
161     }
162
163 }
164
Popular Tags