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 multibox Tag

The multibox tag is also rendered as a check box. However, the multibox tag is mapped to an array of strings or an array of other primitives. There are normally two multibox tags or more being associated with the same property. Here is an example of three multibox tags that are all associated with the interests property. Each multibox tag in this example represents an option.

<html:multibox property="interests">au</html:multibox>Automotive
<html:multibox property="interests">ga</html:multibox>Games
<html:multibox property="interests">sp</html:multibox>Sports

These multibox tags are translated into the following..

<input type="checkbox" name="interests" value="au">Automotive
<input type="checkbox" name="interests" value="ga">Games
<input type="checkbox" name="interests" value="sp">Sports

Each multibox tag has the following format.

<html:multibox property="propertyName">value</html:multibox>label

label is the text to be displayed beside the input element and does not have to be present. The value of the value attribute is sent to the server if the corresponding check box is checked. The label/value pairs for multibox tags may come from a scoped variable referencing a collection (See the sidebar “Managing Options with LabelValueBean”). In turn, the collection may obtain its members from a database or other storage. You then use the JSTL forEach tag (see Chapter 7) or the Struts Logic Tag Library’s iterate tag to iterate the members of the collection. (See Chapter 9)

Note

You can use a select tag in lieu of a multibox tag.

The app03b application exemplifies the use the multibox tag with LabelValueBean. Consider the getCityOptions method in the app03b.form.ProfileForm class.

public ArrayList getCityOptions() {
  ArrayList options = new ArrayList();
  options.add(new LabelValueBean("New York", "1"));
  options.add(new LabelValueBean("Chicago", "2"));
  options.add(new LabelValueBean("San Francisco", "3"));
  options.add(new LabelValueBean("Toronto", "4"));
  options.add(new LabelValueBean("Vancouver", "5"));
  return options;
}

The getCityOptions method serves as a getter method in a JavaBean, thus can be accessed from a JSP. The number of LabelValueBean objects in the ArrayList is not known at design time. However, you can iterate the ArrayList using the JSTL forEach tag (discussed in Chapter 7). For each LabelValueBean instance, you want to produce the following string.

<html:multibox property="cities">value</html:multibox>label

where value is the value of the LabelValueBean instance and label is its label.

You iterate the ArrayList returned by getCityOptions using forEach.

<c:forEach items="${profileForm.cityOptions}" var="city">
...
</c:forEach>

The forEach tag’s items attribute specifies a scoped object’s property. In this case, the scoped object is the action form referenced by profileForm and the property is cityOptions.

For each iteration, forEach retrieves the current element and creates a scoped variable that references the element. The name of the scoped variable is specified by the var attribute (in this case, city). Therefore, from inside the forEach tag, you can use another tag to reference the scoped variable specified by var.

For example, the following code demonstrates the use of forEach with multibox.

<c:forEach items="${profileForm.cityOptions}" var="city">
  <html:multibox property="cities"><c:out value="${city.value}"/>
  </html:multibox><c:out value="${city.label}"/><br/>
</c:forEach>

You can invoke the action in app03b using this URL:

http://localhost:8080/app03b/displayAddProfileForm.do

Figure 3.3 shows what you will see on your browser.

Figure 3.3: Using LabelValueBean

Previous
Next