O'Reilly logo

Jakarta Struts Cookbook by Bill Siggelkow

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

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

<input type="text" name="bar"> ...

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