O'Reilly logo

Java Enterprise in a Nutshell, Third Edition by William Crawford, Jim Farley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

DynaActionForms and the Struts Validator

After the early versions of Struts were released, it became clear that there were certain ActionForm idioms that were so conventional, it would be helpful to introduce ways to declare such ActionForms without requiring Java code to be written. For example, most ActionForms are nothing more than ordinary JavaBeans with getters and setters for very common property types: Strings, ints, Booleans, and so forth. Similarly, validations observed certain conventions: some fields were required, others were required and needed to be sequences of digits forming integers, and so forth.

Enter DynaActionForms and the Struts validator .

org.apache.struts.action.DynaActionForm is a subclass of ActionForm that creates properties on the fly through the Java Reflection API. In your Struts config file, you simply declare that your form is bound to a DynaActionForm, and then write XML to define the properties. Instead of accessing the properties through explicit method calls (e.g., getUsername()), you call a generic get method with the name of the property (for example, get("Username")).

We will focus on a subclass of DynaActionForm that provides for writing even less code (or more XML!) called org.apache.struts.validator.DynaValidatorForm. With DynaValidatorForm, we can not only declare our properties in XML, we can also define standard validations that will be exercised by the Struts validator (a Struts PlugIn).

The example application provides two versions of ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required