Struts Training: Week 4
I missed last week (Week 4, PosgreSQL), but I'm back this week - ready to report. I'm presenting next week on "remember-me" and XDoclet, so I'm working frantically trying to get AppFuse up-to-snuff to use as a lab template. Hopefully, I'll have that done by tomorrow night, as well as the lab and presentation.
Today's presentation is from James Turner on Indexed Properties and Validation. Awesome - I need this in my day job's project starting next week! Good timing, eh?!
So how do you use indexed properties in Forms? Two ways: Simple arrays of strings and arrays of beans (recommended). Here's a good tip - if you're using DynaActionForms, you can access a property in JSTL using {form.map.propertyName}
. I did not know that - thanks James! Lots of good stuff in this one (too much to write down), I hope this preso is available online and a demo app goes with it. To validate simple array of strings, add indexedlistProperty
to your <field> in validation.xml (you must also specify the property). If you're validating beans, use property="propertyName" and indexedListProperty="beanName".
However, do you really want to require all fields of your child beans? No, probably not. You (most likely), just want to require fields if some fields are populated. Struts provides us with the requiredif
validator. No JavaScript validation exists for requiredif at this time. Hmmm, I wonder if XDoclet can generate indexed validation rules. Here's an example of how to do this with the current 1.1 RC1 Release.
<form name="myForm"> <field property="lastName" indexedListProperty="person" depends=""> <arg0 key="label.lastName"/> </field> <field property="firstName" indexedListProperty="person" depends="requiredif"> <arg0 key="label.firstName"/> <var> <var-name>field[0]</var-name> <var-value>lastName</var-value> </var> <var> <var-name>fieldIndexed[0]</var-name> <var-value>true</var-value> </var> <var> <var-name>fieldTest[0]</var-name> <var-value>NOTNULL</var-value> </var> </field> ... </form>
Note that the [0] is NOT an indicator of which indexed property to validate. RequiredIf is powerful but ugly, so James wrote something better. Unfortunately, it's too late for 1.1 and will be added for the 1.2 release. Sounds like most folks that use the Validator will be using a nightly build for awhile ;-). The new validator is called "validwhen," and looks as follows:
<field property="firstName" indexedListProperty="dependents" depends="validwhen"> <arg0 key="dependentListForm.firstName.label"/> <var> <var-name>test</var-name> <var-value>((dependents[].lastName == null) or (dependents[].firstName != null)) </var-value> </var> </field>
Fricken sweet! James is sending me the code - cool! This will make my life soooooo much easier next week. Status of Struts 1.1 from James: they're working on getting the commons packages to a release state. Slides from today will be available at strutskickstart.com later this afternoon.
Posted by Juergen Schmailzl on February 24, 2006 at 07:58 AM MST #
Posted by Matt Raible on February 24, 2006 at 08:00 AM MST #
Posted by Vinay Kumar S on May 24, 2006 at 06:22 AM MDT #
Posted by Matt Raible on May 24, 2006 at 10:46 AM MDT #