FormBeans

A basic Struts component, FormBeans are a special type of DataBean used to carry data from a JSP page/form to an Action control component.  In addition to the normal functions provided by beans, FormBeans also include provisions for validating data entered into Struts forms.

Struts provides two programming models for validation.  Originally, validation was performed via a method call within the formbean class, itself.  A more recent and more extensive form of validation has been added, beginning with Struts 1.1, that relies on a validation plugin.  It provides an extensive set of independent validation tests.  In both cases, messages are stored in a separate resources file.  In the discussion, below, only the first, simpler model is discussed.  For additional details and for explanations of the second model, see the following:  Apache's Sturts Validator Guide, Struts -- Validation and Error handling, and Introducing Struts Validator Framework.

Reference to the Struts API will be helpful in understanding this topic.


Architecture



Example FormBean

// Created by Xslt generator for Eclipse.
// XSL :  not found (java.io.FileNotFoundException:  (No such file or directory))
// Default XSL used : easystruts.jar$org.easystruts.xslgen.JavaClass.xsl

package jbs.abstruts.formbean;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

/** 
 * FormBeanLogin.java created by EasyStruts - XsltGen.
 * http://easystruts.sf.net
 * created on 02-27-2005
 * 
 * XDoclet definition:
 * @struts:form name="FormBeanLogin"
 */
public class FormBeanLogin extends ActionForm {

	// --------------------------------------------------------- Instance Variables

	/** loginName property */
	private String loginName;

	/** loginPassword property */
	private String loginPassword;

	/** loginPasswordConfirm property */
	private String loginPasswordConfirm;

	/** message property */
	private String message;


	// --------------------------------------------------------- Methods

	/** 
	 * Method validate
	 * @param ActionMapping mapping
	 * @param HttpServletRequest request
	 * @return ActionErrors
	 */
	public ActionErrors validate(
		ActionMapping mapping,
		HttpServletRequest request) {

			ActionErrors errors = new ActionErrors();

			// Validate the fields in your form, adding
			// adding each error to this.errors as found, e.g.

			//  must include loginName
			if ((loginName == null) || (loginName.length() == 0)) {
				errors.add("ActionErrors.GLOBAL_ERROR", new ActionMessage("error.login.noname"));
			}
		
			//  must include loginPassword
			if ((loginPassword == null) || (loginPassword.length() == 0)) {
				errors.add("ActionErrors.GLOBAL_ERROR", new ActionMessage("error.login.nopassword"));
			}

			//  if loginPasswortdConfirm present, must match loginPassword
			if ( (mapping.getInput()).equals("/strutsLoginCreate.jsp") )  {
				if ( ! ((loginPasswordConfirm == null) || (loginPasswordConfirm.length() == 0)) ) {
					if ( ! (loginPasswordConfirm.equals(loginPassword)) )
						errors.add("ActionErrors.GLOBAL_ERROR", new ActionMessage("error.loginCreate.nopasswordconfirmmatch"));
				}  else 
						errors.add("ActionErrors.GLOBAL_ERROR", new ActionMessage("error.loginCreate.nopasswordconfirm"));

			}


			return errors;	
			
		}

	/** 
	 * Returns the loginName.
	 * @return String
	 */
	public String getLoginName() {
		return loginName;
	}

	/** 
	 * Set the loginName.
	 * @param loginName The loginName to set
	 */
	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	/** 
	 * Getters & Setters for other properties go here
	 */

}

Error Messages

Error messages, including HTML formatting tags, are defined in a special ApplicationResources file.  An optional header and footer can be defined that bound individual messages.  Specific errors are identified by a label, often expressed in a hierarchical dot notation form, as seen in the example below.

Error messages are displayed within a JSP page through a special <html:errors/>  tag, usually included at the top of the page.  If no ActionErrors object is present in the current scope, no message is displayed.  If one is present, the optional header and footer components are inserted into the JSP followed by the individual messages, as defined in the ApplicationResources file.


Example Resources File

  
# Resources for parameter 'jbs.abstruts.ApplicationResources'
# Project P/ABStruts

# Optional header and footer for <errors/> tag.

errors.header= <H3>Data Errors: <UL>
errors.footer= </UL></H3>

error.login.noname = <LI>No Login name</LI>
error.login.nopassword = <LI>No Password</LI>

error.loginCreate.nopasswordconfirm = <LI>No Password confirm</LI>
error.loginCreate.nopasswordconfirmmatch = <LI>Password and Confirm Password don't match</LI>

error.register.nonamefirst = <LI>No first name</LI>
error.register.nonamelast = <LI>No last name</LI>
error.register.noemail = <LI>No email</LI>

#############################################