001: package org.apache.velocity.tools.struts;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import javax.servlet.http.HttpServletRequest;
023: import javax.servlet.http.HttpSession;
024:
025: import org.apache.struts.action.ActionForm;
026: import org.apache.velocity.tools.view.context.ViewContext;
027:
028: /**
029: * <p>View tool to work with HTML forms in Struts.</p>
030: * <p><pre>
031: * Template example(s):
032: * <input type="hidden" name="$form.tokenName" value="$form.token">
033: * <input type="submit" name="$form.cancelName" value="Cancel">
034: *
035: * Toolbox configuration:
036: *
037: * <tool>
038: * <key>form</key>
039: * <scope>request</scope>
040: * <class>org.apache.velocity.tools.struts.FormTool</class>
041: * </tool>
042: * </pre></p>
043: *
044: * <p>This tool should only be used in the request scope.</p>
045: *
046: * @author <a href="mailto:sidler@teamup.com">Gabe Sidler</a>
047: * @since VelocityTools 1.0
048: * @version $Id: FormTool.java 477914 2006-11-21 21:52:11Z henning $
049: */
050: public class FormTool {
051:
052: // --------------------------------------------- Properties ---------------
053:
054: /**
055: * A reference to the HtttpServletRequest.
056: */
057: protected HttpServletRequest request;
058:
059: /**
060: * A reference to the HtttpSession.
061: */
062: protected HttpSession session;
063:
064: // --------------------------------------------- Constructors -------------
065:
066: /**
067: * Default constructor. Tool must be initialized before use.
068: */
069: public FormTool() {
070: }
071:
072: /**
073: * Initializes this tool.
074: *
075: * @param obj the current ViewContext
076: * @throws IllegalArgumentException if the param is not a ViewContext
077: */
078: public void init(Object obj) {
079: if (!(obj instanceof ViewContext)) {
080: throw new IllegalArgumentException(
081: "Tool can only be initialized with a ViewContext");
082: }
083:
084: ViewContext context = (ViewContext) obj;
085: this .request = context.getRequest();
086: this .session = request.getSession(false);
087: }
088:
089: // --------------------------------------------- View Helpers -------------
090:
091: /**
092: * <p>Returns the form bean associated with this action mapping.</p>
093: *
094: * <p>This is a convenience method. The form bean is automatically
095: * available in the Velocity context under the name defined in the
096: * Struts configuration.</p>
097: *
098: * <p>If the form bean is used repeatedly, it is recommended to create a
099: * local variable referencing the bean rather than calling getBean()
100: * multiple times.</p>
101: *
102: * <pre>
103: * Example:
104: * #set ($defaults = $form.bean)
105: * <input type="text" name="username" value="$defaults.username">
106: * </pre>
107: *
108: * @return the {@link ActionForm} associated with this request or
109: * <code>null</code> if there is no form bean associated with this mapping
110: */
111: public ActionForm getBean() {
112: return StrutsUtils.getActionForm(request, session);
113: }
114:
115: /**
116: * <p>Returns the form bean name associated with this action mapping.</p>
117: *
118: * @return the name of the ActionForm associated with this request or
119: * <code>null</code> if there is no form bean associated with this mapping
120: */
121: public String getName() {
122: return StrutsUtils.getActionFormName(request, session);
123: }
124:
125: /**
126: * <p>Returns the query parameter name under which a cancel button press
127: * must be reported if form validation is to be skipped.</p>
128: *
129: * <p>This is the value of
130: * <code>org.apache.struts.taglib.html.Constants.CANCEL_PROPERTY</code></p>
131: */
132: public String getCancelName() {
133: return org.apache.struts.taglib.html.Constants.CANCEL_PROPERTY;
134: }
135:
136: /**
137: * Returns the transaction control token for this session or
138: * <code>null</code> if no token exists.
139: */
140: public String getToken() {
141: return StrutsUtils.getToken(session);
142: }
143:
144: /**
145: * <p>Returns the query parameter name under which a transaction token
146: * must be reported. This is the value of
147: * <code>org.apache.struts.taglib.html.Constants.TOKEN_KEY</code></p>
148: */
149: public String getTokenName() {
150: return org.apache.struts.taglib.html.Constants.TOKEN_KEY;
151: }
152:
153: }
|