11.1 The Need for a Validation Framework


Chapter 7 discussed how to provide validation logic inside the ActionForm class. The solution presented there requires you to write a separate piece of validation logic for each property that you need to validate. If an error is detected, you have to manually create an ActionError object and add it to the ActionErrors collection. Although this solution works, there are a few problems with the approach.

The first problem is that coding validation logic within each ActionForm places redundant validation logic throughout your application. Within a single web application, the type of validation that needs to occur across HTML forms is very similar. The need to validate required fields, dates, times, and numbers, for example, typically occurs in many places throughout an application. Most nontrivial applications have multiple HTML forms that accept user input that must be validated. Even if you use a single ActionForm for your entire application, you might still end up duplicating the validation logic for the various properties.

The second major problem is one of maintenance. If you need to modify or enhance the validation that occurs for an ActionForm, the source code must be recompiled. This makes it very difficult to configure an application.

The Validator framework allows you to move all the validation logic completely outside of the ActionForm and declaratively configure it for an application through external XML files. No validation logic is necessary in the ActionForm, which makes your application easier to develop and maintain. The other great benefit of the Validator is that it's very extensible. It provides many standard validation routines out of the box, but if you require additional validation rules, the framework is easy to extend and provides the ability to plug in your own rules (again without needing to modify your application).



Programming Jakarta Struts
Programming Jakarta Struts, 2nd Edition
ISBN: 0596006519
EAN: 2147483647
Year: 2003
Pages: 180

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net