Previous Section
Table of Contents
Next Section


Chapter 6: Validator

Overview

One of the major benefits of using the Struts framework is its built-in interface for performing data validations on incoming form data. As discussed in Chapter 4, upon submitting an HTML form, Struts captures the form data and uses it to populate one of your application’s ActionForm subclasses (Form Bean) assigned to the form. The Form Bean’s validate( ) method is then called to perform any necessary validation of the incoming data. If any validations fail, the HTML form is redisplayed so that the invalid data can be corrected. Otherwise, processing continues. This simple interface alleviates much of the headache associated with handling data validation, allowing you to focus on validation code and not the mechanics of capturing data and redisplaying incomplete or invalid data.

Struts’ built-in validation interface, however, still has its shortcomings. Often, for example, validation code is heavily duplicated throughout an application because many fields require the same validation logic. Any change in the validation logic for similar fields requires code changes in several places, as well as recompilation of the affected code. To solve this problem and to enhance Struts’ validation interface, David Winterfeldt created the Validator framework as a third-party add-on to Struts. Validator was later integrated into the core Struts code base and has since been detached from Struts and is now a stand-alone Jakarta Commons project that can be used with or without Struts. Although Validator is an independent framework again, Struts still comes packaged with it and it is seamlessly integrated.

The Validator framework comes prepackaged with several validation routines, making the transition from hard-coded validation logic painless. Instead of coding validation logic in each Form Bean’s validate( ) method, with Validator you use an XML configuration file to declare the validations that should be applied to each Form Bean. If you need a validation not provided by Validator, you can plug your own custom validations into Validator. Additionally, Validator supports both server-side and client-side (JavaScript) validations whereas Form Beans only provide a server-side validation interface.



Previous Section
Table of Contents
Next Section