Migrating an application to the Struts Validator
While many Struts applications are written to use the Struts Validator from the get-go, most start out using their own routines before moving to the Validator. In this section, we walk through migrating a simple ActionForm
validate
method to its Struts Validator counterpart. The point of the exercise is not the example
validate
method, which is trivial, but the process of moving the method to the Struts Validator.
- In chapter 11, we followed a similar process by migrating some example pages to Tiles.
Setting up the Validator framework
Setting up the validator varies slightly between Struts 1.0 and 1.1, but works just as well with either version.
Note
Before you begin, make an extra backup of everything, regardless of how many backups are made in the normal course. Migrating to the Validator can be tricky at first, and, realistically, you may need to make more than one pass before everything clicks into place. So, be ready to roll back and try again. 'Nuff said.
Struts 1.0
If you haven't done so, the first step is to install the Validator package and load the Validator servlet through your application's deployment descriptor. (The Validator servlet is just a resource loader and so does not conflict with the Tiles servlet.) Then, test your application to be sure all is well by clicking through a few pages.
- The Blank application for Struts 1.0 on the book's website [Husted] includes an empty Validator configuration file and sample setup.
Struts 1.1
The Validator is integrated with Struts 1.1. The steps for enabling the Validator in Struts 1.1 are covered in section 4.9 of this book.
Testing the default configuration
Set the debug and detailparameters in the
deployment descriptor (web.xml) to level 2, and restart the
application. Check the log entries carefully for any new error
messages. Run any unit tests and click through the application
to confirm that operation is still nominal.
Reviewing your validations
With the Validator up and running, the next step is to take a good hard look at your
validate
methods. Identify which will correspond to a standard Struts Validator validation and which will have to be handled on a custom basis. The Validator is not an either/or proposition. You can continue to use the ActionForm
validate
method to handle some things, and the Validator to handle the rest.
- Listing 12.13 shows a code skeleton that calls the Struts Validator and then tries any custom validations.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.