validator by night

I was trying to look at the validator plugin for struts. Struts counted the number of its beginners by one, ya. that’s me!!

Previously I have a simple collection of jsps and classes. something like get employee number, and display the employee details. employee number is in the form of xxx-xx-xxxx, where an x is a number. I had my form bean that has a validate method, that checks whether the number has been entered and the number entered is in the specified format. see below!

// Validate format of social security number.
private static boolean isValidSsNum(String ssNum) {
if (ssNum.length() < 11) {
return false;

for (int i = 0; i < 11; i++) {
if (i == 3 || i == 6) {
if (ssNum.charAt(i) != ‘-‘) {
return false;
} else if (“0123456789”.indexOf(ssNum.charAt(i)) == -1) {
return false;

return true;

if it returns false, then new ActionError will be added errors and returned from validate() method. then comes the validator chapter 🙂 actually, they tries to keep validation routines in a centralised location and call whenever there is a need.

Though it took time to keep track of the control flow, i feel it is a good practice, which i want to follow. They define the validation rules in an xml (validator-rules.xml) and map these rules to actions with another xml (validation.xml). Necessary changes have been done to FormBeans/DynaBean declarations.

Because of the validator plugin, both client-side and server-side validations becomes easier now.

Forexample, validator-rules.xml has an validator name ‘required’, which check the existance of data in form fields. the javascript code needed to perform this has been written in the xml file itself. So, it is directly embedded in the jsp whereever needed and executed.

Each definition is declared with validator tag. it is used to assign logical name to the routine with name attribute, and class and method for the routine.

Changes to Form Beans
If you have a Form Bean that extends ActionForm, make it to extend ValidatorForm. remove the reset() and validate() methods since they are implicitly given by the Validator framework.

import org.apache.struts.validator.ValidatorForm; public class MyForm extends ValidatorForm { }

The Form bean configuration in struts-config.xml is not changed.


The logical name ‘myForm’ is the one we will use for making validations. keep a note of that!

Then lets go for validation.xml, where we will map the forms with validations.

property=”username” depends=”required”>

If you have used Dynamic Form Beans, then your entries may go like this.

yes, you are right. you will be using DynaValidatorForm instead of DynaActionForm.

now coming back to my page… I need to do

1. Change the form to extend ValidatorForm (done!)
2. add a validator-rules.xml file. (yes, it is there )
3. create a validation.xml file with the following entry
I need to validate whether my ssNum is in the form of xxx-xx-xxxx format. so new entry has been added to validation.xml

property=”ssNum” depends=”mask”> mask ^d{3}-d{2}-d{4}$
4. Add the validator plugin to struts-config.xml file

value=”/WEB-INF/validator-rules.xml, /WEB-INF/validation.xml”/>

has been added.

5. Add validation error message to Application Resources. (done)
6. recomplile, repackage and run the webapp!! (done)
it is working fine 🙂

yes. got a basic idea of validator module. I need to go through how to embed javascript for client side validation and how to internationalise the error messages. then validator may be complete!

See You!

By default, struts comes with many validations preconfigured. please see for more det

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s