Home

This Chapter
-Chapter 9: The Logic Tag Library
-The Value Comparison Tags
-The Substring Matching Tags
-The Presentation Location Tags
-The Collection Utility Tag
-Summary

Table of Contents
-Introduction
-Chapter 1: Model 2 and Struts
-Chapter 2: Input Validation with Action Forms
-Chapter 3: The HTML Tag Library
-Chapter 4: Input Validation and Data Conversion
-Chapter 5: The Validator Plugin
-Chapter 6: The Expression Language
-Chapter 7: JSTL
-Chapter 8: The Bean Tag Library
-Chapter 9: The Logic Tag Library
-Chapter 10: Struts-EL, Nested, selectLabel
-Chapter 11: Message Handling and Internationalization
-Chapter 12: The Tiles Framework
-Chapter 13: Securing Struts Applications
-Chapter 14: The Config Object
-Chapter 15: The Persistence Layer
-Chapter 16: Object Caching
-Chapter 17: File Upload and File Download
-Chapter 18: Paging and Sorting
-Chapter 19: Preventing Double Submits
-Chapter 20: Early HttpSession Invalidation
-Chapter 21: Decorating Request Objects
-Chapter 22: How Struts Works

Previous
Next

 

The Collection Utility Tag

There is only one tag in this category, iterate. This tag is used to iterate a collection. For each iteration, the iterate tag creates a scoped variable that references the element being iterated. Table 9.5 lists the attributes of this tag.

Attribute Description
id A required attribute that specifies the name of the page-scoped variable that is created in each iteration to reference the element being iterated.
collection Specifies the scoped variable that references the collection to be iterated over.
name Specifies the scoped variable that references a collection or an object that contains a collection. If the latter is the case, the property attribute must be present.
property Specifies the property of the scoped object specified by the name attribute. The property must return a collection.
scope The scope within which the scoped object named by the name property will be searched for. The possible values are page, request, session, application, or any scope (default).
length The maximum number of elements to be iterated through. If the length attribute is not present, the iterate tag iterates through all the elements.
offset The zero-based index that specifies the number of first elements to be skipped. If the value of this attribute is 3, for example, the first four elements will not be iterated.
type The type of the element to be iterated. If not present, no type conversion is performed.

Table 9.5: The iterate tag’s attributes.

As an example, consider the execute method of the TestLogicAction class in the app09a application, which is given in Listing 9.1.

Listing 9.1: The execute method of the TestLogicAction class

public ActionForward execute(ActionMapping mapping, ActionForm form,
  HttpServletRequest request, HttpServletResponse response)
  throws Exception {
  request.setAttribute("position", "StrutsProgrammer");

  ArrayList addresses = new ArrayList();

  AddressTO address1 = new AddressTO();
  address1.setStreetAddress("315 Avoca Street");
  address1.setCity("Albany");
  address1.setState("NY");
  address1.setZipCode("12110");
  ArrayList phones = new ArrayList(2);
  phones.add("432 89894949");
  phones.add("432 84839283");
  address1.setPhones(phones);
  addresses.add(address1);

  AddressTO address2 = new AddressTO();
  address2.setStreetAddress("144 Tiara Road");
  address2.setCity("Sydney");
  address2.setState("NSW");
  address2.setZipCode("2000");
  phones = new ArrayList(2);
  phones.add("02 89898999");
  phones.add("02 87282911");
  address2.setPhones(phones);
  addresses.add(address2);

  request.setAttribute("addressList", addresses);

  return mapping.findForward("success");
}

The execute method creates a scoped variable addressList that references the ArrayList addresses. You can iterate the content of the collection by using the following iterate tag.

<logic:iterate id="address" name="addressList">
Street Address:<bean:write name="address"
  property="streetAddress"/><br/>

  City:<bean:write name="address" property="city"/><br/>
  State:<bean:write name="address" property="state"/><br/>
  Zip Code:<bean:write name="address" property="zipCode"/><br/>

  <logic:iterate id="phone" name="address" property="phones">
      Phone:<bean:write name="phone"/><br/>
  </logic:iterate>
  <hr/>
</logic:iterate>

Note, you can even iterate an element of iteration by using an iterate tag inside the main iterate tag.

Previous
Next