logo
header art

Building an Enterprise-Ready Web Application with Struts

June 23, 2005

Form Beans

ActionForms are beans that represent an HTML form. In struts-config.xml, we defined a single ActionFormBean: ShowRecipeForm.

001 /*
002  * ShowRecipeForm.java created on Apr 27, 2005 at 2:21:36 PM
003  
004  * strutsExample is a simple web application that allows a user to search and
005  * view a set of recipes. Copyright (C) 2005 SkillFusion Software
006  
007  * This program is free software; you can redistribute it and/or modify it under
008  * the terms of the GNU General Public License as published by the Free Software
009  * Foundation; either version 2 of the License, or (at your option) any later
010  * version.
011  
012  * This program is distributed in the hope that it will be useful, but WITHOUT
013  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
014  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
015  * details.
016  
017  * You should have received a copy of the GNU General Public License along with
018  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
019  * Place - Suite 330, Boston, MA 02111-1307, USA
020  */
021 package com.skillfusion.examples.struts.forms;
022 
023 import org.apache.commons.logging.Log;
024 import org.apache.commons.logging.LogFactory;
025 
026 import javax.servlet.http.HttpServletRequest;
027 
028 import org.apache.struts.action.ActionErrors;
029 import org.apache.struts.action.ActionForm;
030 import org.apache.struts.action.ActionMapping;
031 import org.apache.struts.action.ActionMessage;
032 import org.apache.struts.action.ActionMessages;
033 
034 /**
035  * This is a simple form bean to do some validation on the data processed by
036  <code>ShowRecipeAction</code>.
037  
038  @author hank
039  */
040 public final class ShowRecipeForm extends ActionForm {
041     /**
042      * Logger for this class.
043      */
044     private static final Log LOGGER = LogFactory.getLog(ShowRecipeForm.class);
045 
046     /**
047      * The underlying field for the <tt>Name</tt> input on the <tt>.jsp</tt>.
048      */
049     private String name;
050 
051     /**
052      * Retrieves a reference to <code>name</code>.
053      
054      @return The <code>String</code> stored in <code>name</code>.
055      */
056     public String getName() {
057         if (LOGGER.isTraceEnabled()) {
058             LOGGER.trace("getName() - start");
059         }
060 
061         if (LOGGER.isTraceEnabled()) {
062             LOGGER.trace("getName() - end - return value = " this.name);
063         }
064         return this.name;
065     }
066 
067     /**
068      * Sets the value of <code>name</code>.
069      
070      @param newName
071      *            A <code>String</code> containing the new value for
072      *            <code>name</code>.
073      */
074     public void setName(final String newName) {
075         if (LOGGER.isTraceEnabled()) {
076             LOGGER.trace("setName(String newName = " + newName + ") - start");
077         }
078 
079         this.name = newName;
080 
081         if (LOGGER.isTraceEnabled()) {
082             LOGGER.trace("setName(String newName = " + newName + ") - end");
083         }
084     }
085 
086     /**
087      * Simple form bean validation, this method just makes sure that the name is
088      * neither <tt>null</tt> or an empty <code>String</code>. In the
089      * future, this method may be replaced by using the declarative validation
090      * approach afforded by Validator.
091      
092      @param mapping
093      *            The mapping used to select this instance
094      @param request
095      *            The HTTP request we are processing
096      @return A collection of the validate errors we found.
097      @see org.apache.struts.action.ActionForm#validate(org.apache.struts.action.ActionMapping,
098      *      javax.servlet.http.HttpServletRequest)
099      */
100     public ActionErrors validate(final ActionMapping mapping,
101             final HttpServletRequest request) {
102         if (LOGGER.isTraceEnabled()) {
103             LOGGER
104                     .trace("validate(ActionMapping mapping = " + mapping
105                             ", HttpServletRequest request = " + request
106                             ") - start");
107         }
108 
109         // Set up our return variable
110         ActionErrors toReturn = new ActionErrors();
111 
112         // Merge in the results of our superclass's validate function
113         toReturn.add(super.validate(mapping, request));
114 
115         // If name is null (or empty string), add an error message
116         if (null == this.getName() || == this.getName().length()) {
117             toReturn.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
118                     "error.required.name"));
119         }
120 
121         ActionErrors returnActionErrors = super.validate(mapping, request);
122         if (LOGGER.isTraceEnabled()) {
123             LOGGER.trace("validate(ActionMapping mapping = " + mapping
124                     ", HttpServletRequest request = " + request
125                     ") - end - return value = " + returnActionErrors);
126         }
127         return returnActionErrors;
128     }
129 }

Each element of the HTML form corresponds to a property on the bean. In the case of ShowRecipeForm, that means it has a single property: name. In addition to the getter and setter for name, there is also a validate(ActionMapping, HttpServletRequest) function, which begins on line 100. This method is invoked when you submit the HTML form corresponding to this ActionForm. There is a default implementation that does no actual validation and is suitable if you wish to skip validation or set up declarative validation, but you can also override it to perform the actual validation, as we have here. While declarative validation is the most popular choice, setting it up requires more overhead than can be justified for just one form. In ShowRecipeForm, we set up the validate() method to add an error message to the request if the user did not enter a name on the HTML form.