Dynamic forms and validation is definitely a useful part of the Sruts framework. The material on it unfortunately is also probably the most badly written part of Cavaness' Programming Jakarta Struts. These notes are meant to complement, not replace, what's in Cavaness. These notes will make no sense on their own.
DynaActionForm's are meant to solve a simple problem: why define
subclass after subclass of
ActionForm when most forms are nothing
more than a set of simple fields, typically strings?
DynaActionForm instead of a custom subclass of
is relatively straightforward. You need to make changes in two places:
<form-bean>to be an
org.apache.struts.action.DynaActionForminstead of some subclass of
Actionsubclass that uses your form bean:
<form-bean> in struts-config.xml is
straightforward. See pages 191-192 of Cavaness.
Action subclass, wherever you used to write things like
Map interface, so
get() with a key
is used to retrieve data.
Object, so if you need a
String, you need to downcast, e.g.,order.setPrice(Double.parseDouble((String) form.get("price")));
Those should be the only changes you need to make to use a
Unfortunately, if you use a
DynaActionForm, and you want to validate
the field values, you have to create a subclass of
which is what you were trying to avoid in the first place.
Fortunately, there's a subclass of
DynaActionForm that gets validation
rules from an XML file. Using this validation framework involves four separate
validator-rules.xmlfrom the Struts lib/ directory to your webapp's WEB-INF/ directory
validation.xmlfile in your
WEB-INF/directory that says what your form field values have to look like
Cavaness' example of the
<plug-in> element on page 268 has
a typo. He tells you to create
validation.xml, but he wrote
To be safe, copy the
<plug-in> element from
in the example
struts-validator.war file in the Struts home directory.
Similarly, the easiest way to create
validation.xml is to copy
!DOCTYPE elements from
struts-validator.war. Then create use the following pattern
for specifying rules for the fields:
<form-validation> <formset> <form name="form-name"> <field property="field-name" depends="required,mask"> <arg0 key="resource-label"/> <var> <var-name>mask</var-name> <var-value>regular-expression</var-value> </var> </field> ...repeat for each field... </form> </formset> </form-validation>
This XML syntax has some of the worst naming conventionsI have seen in 35 years of programming. Just use them, don't try to explain them.
<depends>specifies the names of the rules to apply to this field. Some common rules are
required-- the field is required
mask-- the field has to match the regular expression defined the
<var>element of that name below.
intRange-- the field has to be an integer with a certain range
<arg0>specifies the resource key for the string to use when printing a validation error message about this field. Usually you use the same resource key that is used to label the field in the HTML or JSP form.
<var>defines variables. The most common use is to define a regular expression that the field value has to match.
The book shows an example expression for a phone number. There are many online tutorials on regular expressions and many discussions on how to validate an email address, a Zip code, etc. There's also online examples of validation forms here.
Send comments to email@example.com