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 ActionForm
s without requiring Java code to
be written. For example, most ActionForm
s are nothing more than ordinary
JavaBeans with getters and setters for very common property types:
String
s, int
s, Boolean
s, 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 DynaActionForm
s 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 ...
Get Java Enterprise in a Nutshell, Third Edition 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.