From 879f0a5aaab90eaec6e4aaa41ffff256230f2dab Mon Sep 17 00:00:00 2001 From: mdb Date: Wed, 31 Oct 2001 11:07:55 +0000 Subject: [PATCH] Created a form tool for generating form fields that automatically inherit the values from the request parameters. Modified dispatcher servlet to put one in the context for every request. git-svn-id: https://samskivert.googlecode.com/svn/trunk@399 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../velocity/DispatcherServlet.java | 11 +- .../com/samskivert/velocity/FormTool.java | 173 ++++++++++++++++++ 2 files changed, 183 insertions(+), 1 deletion(-) create mode 100644 projects/samskivert/src/java/com/samskivert/velocity/FormTool.java diff --git a/projects/samskivert/src/java/com/samskivert/velocity/DispatcherServlet.java b/projects/samskivert/src/java/com/samskivert/velocity/DispatcherServlet.java index 1ea3148d..fd3cbcc7 100644 --- a/projects/samskivert/src/java/com/samskivert/velocity/DispatcherServlet.java +++ b/projects/samskivert/src/java/com/samskivert/velocity/DispatcherServlet.java @@ -1,5 +1,5 @@ // -// $Id: DispatcherServlet.java,v 1.2 2001/10/31 09:45:23 mdb Exp $ +// $Id: DispatcherServlet.java,v 1.3 2001/10/31 11:07:55 mdb Exp $ // // samskivert library - useful routines for java programs // Copyright (C) 2001 Michael Bayne @@ -232,6 +232,10 @@ public class DispatcherServlet extends VelocityServlet ictx.put(MSGRESOLVER_KEY, mrslv); } + // create a form tool for use by the template + FormTool form = new FormTool(req); + ictx.put(FORMTOOL_KEY, form); + // resolve the appropriate logic class for this URI and // execute it if it exists String path = req.getServletPath(); @@ -367,6 +371,11 @@ public class DispatcherServlet extends VelocityServlet */ protected static final String MSGRESOLVER_KEY = "i18n"; + /** + * This is the key used to store the form tool in the context. + */ + protected static final String FORMTOOL_KEY = "form"; + /** The servlet parameter key specifying the application class. */ protected static final String APP_CLASS_PROPS_KEY = "app_class"; diff --git a/projects/samskivert/src/java/com/samskivert/velocity/FormTool.java b/projects/samskivert/src/java/com/samskivert/velocity/FormTool.java new file mode 100644 index 00000000..4eb25fa4 --- /dev/null +++ b/projects/samskivert/src/java/com/samskivert/velocity/FormTool.java @@ -0,0 +1,173 @@ +// +// $Id: FormTool.java,v 1.1 2001/10/31 11:07:55 mdb Exp $ +// +// samskivert library - useful routines for java programs +// Copyright (C) 2001 Michael Bayne +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.samskivert.velocity; + +import javax.servlet.http.HttpServletRequest; + +import com.samskivert.servlet.util.HTMLUtil; +import com.samskivert.servlet.util.ParameterUtil; +import com.samskivert.util.StringUtil; + +/** + * The form tool can be placed into an invocation context and used by the + * template to create form elements which automatically inherit form + * values which were provided to the page at request time. + * + * For example: + * + *
+ * Please enter your name: $form.text("name", 40) $form.submit("submit")
+ * 
+ * + * If the servlet was invoked with a value for "name", it will be + * automatically filled into the "name" form field when it is requested. + */ +public class FormTool +{ + /** + * Constructs a form tool that will use the supplied HTTP servlet + * request object to prefetch form values. + */ + public FormTool (HttpServletRequest req) + { + _req = req; + } + + /** + * Creates a text input field with the specified name and no extra + * arguments or default value. + * + * @see #text(String,String,String) + */ + public String text (String name) + { + return text(name, "", ""); + } + + /** + * Creates a text input field with the specified name and the + * specified extra arguments with no default value. + * + * @see #text(String,String,String) + */ + public String text (String name, String extra) + { + return text(name, extra, ""); + } + + /** + * Creates a text input field with the specified name, extra arguments + * and default value. For example, a call to text("foo", + * "size=\"5\" maxLength=\"8\"", "bar") would result in an + * input field looking like: + * + *
+     * <input type="text" name="foo" value="bar" size="5" maxLength="8">
+     * 
+ * + * Assuming the foo parameter had no pre-existing value. + */ + public String text (String name, String extra, Object defaultValue) + { + StringBuffer buf = new StringBuffer(); + buf.append(""); + return buf.toString(); + } + + /** + * Constructs a submit element with the specified parameter name and + * the specified button text. + */ + public String submit (String name, String text) + { + StringBuffer buf = new StringBuffer(); + buf.append(""); + return buf.toString(); + } + + /** + * Constructs a hidden element with the specified parameter name where + * the value is extracted from the appropriate request parameter. + */ + public String hidden (String name) + { + return hidden(name, ""); + } + + /** + * Constructs a hidden element with the specified parameter name where + * the value is extracted from the appropriate request parameter + * unless there is no value in which case the supplied default value + * is used. + */ + public String hidden (String name, Object defaultValue) + { + return fixedHidden(name, getValue(name, defaultValue)); + } + + /** + * Constructs a fixed hidden element with the specified parameter name + * and value. The contents of the parameter of the same name are + * ignored when creating this element. + */ + public String fixedHidden (String name, String value) + { + StringBuffer buf = new StringBuffer(); + buf.append(""); + return buf.toString(); + } + + /** + * Fetches the requested value from the servlet request and entifies + * it appropriately. + */ + protected String getValue (String name, Object defaultValue) + { + String value = ParameterUtil.getParameter(_req, name, true); + if (StringUtil.blank(value)) { + value = String.valueOf(defaultValue); + } + return HTMLUtil.entify(value); + } + + /** A reference to the servlet request in use by this form tool. */ + protected HttpServletRequest _req; +}