Docsity

Creating a Custom Validator in Struts 1.3: A Step-by-Step Guide, Lecture notes of Java Programming

Toggle sidebar
bg1
Creating Custom Validators in STRUTS
In this tutorial you will learn how to develop Custom Validators in your Struts 1.3 applications.
Struts Validator framework provides many validation rules that can be used in the web
applications. If you application needs special kind of validation, then you can extend the
validator framework to develop your own validation rule.
The client-side validation in Struts is well known. Here are some of the available features:
required
requiredif
validwhen
minlength
maxlength
mask
byte
short
integer
long
float
double
byteLocale
shortLocale
integerLocale
longLocale
floatLocale
doubleLocale
date
intRange
longRange
floatRange
doubleRange
creditCard
email
url
These are found in the validator-rules.xml inside the <validator> tags. The validator-rules.xml
file is found in the commons-validator jar.
Let us know create a new validator for entering the name field of a form. The form should
accept only "administrator" for the name field. To accomplish this edit the validator-rules.xml
and add the following code under the <global> tag:
<validator name="matchname"
classname="org.apache.struts.validator.FieldChecks"
method="validateName"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionMessages,
org.apache.commons.validator.Validator,
docsity.com
pf3
pf4
pf5

Partial preview of the text

Download Creating a Custom Validator in Struts 1.3: A Step-by-Step Guide and more Lecture notes Java Programming in PDF only on Docsity!

Creating Custom Validators in STRUTS

In this tutorial you will learn how to develop Custom Validators in your Struts 1.3 applications.

Struts Validator framework provides many validation rules that can be used in the web

applications. If you application needs special kind of validation, then you can extend the

validator framework to develop your own validation rule.

The client-side validation in Struts is well known. Here are some of the available features:

 required

 requiredif

 validwhen

 minlength

 maxlength

 mask

 byte

 short

 integer

 long

 float

 double

 byteLocale

 shortLocale

 integerLocale

 longLocale

 floatLocale

 doubleLocale

 date

 intRange

 longRange

 floatRange

 doubleRange

 creditCard

 email

 url

These are found in the validator-rules.xml inside the tags. The validator-rules.xml

file is found in the commons-validator jar.

Let us know create a new validator for entering the name field of a form. The form should

accept only "administrator" for the name field. To accomplish this edit the validator-rules.xml

and add the following code under the tag:

javax.servlet.http.HttpServletRequest" msg="errors.name">

To understand the above code:

 matchname is the new validator we are creating; use can use anything you want (e.g.

matchAdmin) remembering that this will be used in another file which will be described

later

 the error message issued in the browser has the key errors.name; you can have any name

here like errors.admin; once again this will be explained later

 the Java Script function to call is declared in the method attribute of the validator tag;

in the above it is called validateName; you can have any valid Java Script function name

(e.g. validateAdmin)

 the Java Script to process this tag is declared inside CDATA; note that the function name

should match EXACTLY with the name declared in the method attribute of the validator

tag

 the field.value != "administrator" is where we actually test the value entered in the

browser; you can substitute any string in the place of "administrator"; also you can do

more sophisticated checking (e.g. replace all blanks; check for upper/lower case, etc.) if

you are an experienced Java Script programmer

Create a JSP file as follows:

<%@ taglib uri="struts-bean.tld" prefix="bean" %> <%@ taglib uri="struts-html.tld" prefix="html" %>

Administrator Test

This application shows the use of Struts Validator. The following form contains fields that are processed by Struts Validator. Fill in the form and see how JavaScript generated by Validator Framework validates the form.

Name

Save

Cancel

Then we create the success.jsp

<% out.println("SUCCESS") %>

Then we create the Java Class for the AdminForm

package test;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.*;

  • Form bean for the Admin Entry Screen.

*/ public class AdminForm extends ActionForm { private String name=null;

public void setName(String name){ this.name=name; }

public String getName(){ return this.name; }

  • Reset all properties to their default values.
  • @param mapping The mapping used to select this instance
  • @param request The servlet request we are processing */ public void reset(ActionMapping mapping, HttpServletRequest request) { this.name=null; }

/**

  • Reset all properties to their default values.
  • @param mapping The mapping used to select this instance