Home

This Chapter
-Chapter 3: The HTML Tag Library
-The html Tag
-The base Tag
-The form Tag
-The text, password, hidden, textarea Tags
-The submit and cancel Tags
-The reset Tag
-The button Tag
-The checkbox Tag
-The multibox Tag
-The radio Tag
-The select Tag
-The option Tag
-The options Tag
-The optionsCollection Tag
-The link Tag
-The img Tag
-The rewrite Tag
-The frame Tag
-The image Tag
-The xhtml 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 optionsCollection Tag

Like the options tag, the optionsCollection tag can get its label/value pairs from a collection or an object that contains a collection. In either case, the collection or the object containing a collection must be a scoped object so that it can be accessed by custom tags. Two applications, app03c and app03d, provide examples of how to use optionsCollection in the two scenarios.

Using optionsCollection with An Object Containing A Collection

The productForm action form in the app03c application has the categoryList property:

public ArrayList getCategoryList() {
  ArrayList colorList = new ArrayList(3);
  colorList.add(new LabelValueBean("Computer", "Com"));
  colorList.add(new LabelValueBean("Camera", "Cam"));
  colorList.add(new LabelValueBean("Mobile Phone", "MP"));
  return colorList;
}

You can then use this to populate a select tag with the categoryList property:

<html:select property="category">
  <html:optionsCollection name="productForm"
    property="categoryList"/>
</html:select>

Alternatively, if the collection is in the containing object, you can remove the name property. In this case, the name attribute takes the value of the enclosing action form.

<html:select property="category">
  <html:optionsCollection property="categoryList"/>
</html:select>

Both will be translated into:

<select name="category">
  <option value="Com">Computer</option>
  <option value="Cam">Camera</option>
  <option value="MP">Mobile Phone</option>
</select>

Using optionsCollection with A Collection

The app03d application demonstrates how to use optionsCollection with a collection. Suppose you have the following code in an application listener that creates an ArrayList of LabelValueBean objects named productList and adds it to the ServletContext object:

ArrayList productList = new ArrayList(3);
productList.add(new LabelValueBean("Computer", "co"));
productList.add(new LabelValueBean("Television", "tv"));
productList.add(new LabelValueBean("Radio", "ra"));
servletContext.setAttribute("productList", productList);

You can then use this select tag that gets its options from the productList collection in the ServletContext object.

<html:select property="category">
  <html:optionsCollection name="productList" label="label"
    value="value"/>
</html:select>

Or, if you are getting the label from a property named label and the value from a property named value in the object that represents an option, you can remove the label and value attributes:

<html:select property="category">
  <html:optionsCollection name="productList"/>
</html:select>

In the app03d application, the select and optionsCollection tags will be rendered as:

<select name="category">
  <option value="co">Computer</option>
  <option value="tv">Television</option>
  <option value="ra">Radio</option>
</select>

Previous
Next