Chapter 11. Struts 455
11.6 Struts validator framework
The Struts framework allows input validation to occur inside the ActionForm. To
perform validation on data passed to a Struts application, developers must code
special validation logic inside each ActionForm class. Although this approach
works, it has some serious limitations. The Validator framework allows you to
declaratively configure validation routines for a Struts application without
programming special validation logic. The Validator has become so popular and
widely used by Struts developers that it has been added to the list of Jakarta
To use Validator framework, we need to do a number of simple actions:
1. Define fields to validate.
2. Add the plug-in.
3. Add validator_rules.xml.
4. Configure error messages.
5. Configure validation rules.
6. Modify your old validations.
11.7 Struts validation sample
The sample application for Sal301 used Struts validations on the server side. For
this project our Sal404 application sample uses validations on the client side.
One reason for using client-side validations is that you will have less traffic on the
The steps to implement validation in our Sal404 sample are:
1. Define the fields for your validations.
For our project, we focused on the Register Form. The fields we decided to
validate are shown in Table 11-1.
Table 11-1 Fields to validate in the register form
Field Validation Field Field
User name* Character 20 Address name* char <20
password* Char >6 <20 Street* char <100
confirm password* Equal to password Unit char < 10
Title Char 5 Building char < 10
First Name* Char < 40 PO-Box char < 10
456 WebSphere Application Server - Express V6 Developers Guide and Development Examples
In Table 11-1 on page 455 all the fields marked with * are required.
Figure 11-4 shows the fields as laid out on the user registration form in our
sample application.
Figure 11-4 Fields on the user registration form
2. The next step is to add the Validator plugin to the Struts config file. Using the
Project Explorer view, navigate to the struts-config.xml file found in SAL404R
Web Project Struts default struts-config.xml.
Last Name* Char < 40 City char <
Email* email valid Post/Zip Code* decimal < 6
Web site Country* char = 4
Phone Number ^\(?(\d{3})\)?[-|
]?(\d{3})[-| ]?(\d{4})
State char < 40
Aditional Info >=0 Char < 100
Field Validation Field Field
Chapter 11. Struts 457
3. Open the Struts configuration editor by double-clicking the struts-config.xml
4. Click the Plug-ins tab. Figure 11-5 shows the Struts configuration editor open
on the plug-ins tab.
Figure 11-5 Struts plug-ins
5. Click Add and choose the validator plugin as shown in Figure 11-6 on
page 458.
458 WebSphere Application Server - Express V6 Developers Guide and Development Examples
Figure 11-6 Add ValidatorPlugin
6. Click OK.
7. Under the heading Plug-in Mapping Extensions click Add to add a property
called pathnames with a value of /WEB-INF/validator-rules.xml,
/WEB-INF/validation.xml. See Figure 11-7 on page 459.
Chapter 11. Struts 459
Figure 11-7 Plugin mapping extensions
8. Save the changes to the Struts config file.
9. Add the validator-rules.xml to your Web project. Right-click the WEB-INF
folder in the Web project and choose Import.
10.Select to import from the File system and click Next.
11.Click Browse and select the validator-rules.xml example that is supplied
with Rational Software Development Platform. This can be found in the
12.Click Finish to import the file. See Figure 11-8 on page 460.
Tip: You can add the plug-in manually to the struts-config.xml by entering the
following text:
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames" value="/WEB-INF/validator-rules.xml,
/WEB-INF/validation.xml"/> </plug-in>

Get WebSphere Application Server - Express V6 Developers Guide and Development Examples now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.