3.4. Using Indexed Properties on Forms
Problem
You want to create a set of input fields on a form that corresponds to the indexed properties of a bean.
Solution
Use the indexed attribute on tags in the
Struts
html tag library to generate the property value:
<html:form action="TestOneAction"><p>
<logic:iterate name="MyForm" property="stringArray"
id="stringValue" indexId="ctr">
<br/>
<html:text property="stringArray" indexed="true"/>
</logic:iterate>
</html:form>Discussion
As shown in Recipe 3.3, accessing indexed
properties for display purposes is easy. However, using indexed
properties in a form can be tricky. If the name of the generated
input field is not formatted correctly, Struts can't
populate the ActionForm when the HTML form is
submitted. Struts populates the ActionForm with
values from the HTTP request using the Jakarta Commons BeanUtils package.
Specifically, the BeanUtils.populate( ) method
loads the ActionForm from the HTTP request data
sent when the form is submitted.
For indexed properties,
BeanUtils.populate(
) uses the name of the request parameter to determine the
proper setter method to call on the ActionForm.
Table 3-3 illustrates how different form input
field names are processed. The table shows the HTML tag, the
corresponding HTTP request name/value pair, and the method that is
called on the ActionForm when the request is
processed.
Table 3-3. ActionForm population samples
|
HTML form input tag |
Generated request pair |
Resulting method call |
|---|---|---|
|
|