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)
The multibox tag and several other tags use one or many label/value pairs as the source of options. The org.apache.struts.util.LabelValueBean class is a convenience class for encapsulating a label and a value. The LabelValueBean class is a JavaBean with two String properties, label and value. You can create a LabelValueBean object by using one of its constructors.
public LabelValueBean() public LabelValueBean(java.lang.String label, java.lang.String value)
You normally create multiple LabelValueBean instances and put them in an ArrayList or some other collection. You then make that collection a scoped object so that it can be accessed from a JSP. Alternatively, you can put the collection (containing LabelValueBean objects) in another object and make the latter a scoped object.
Here are some rules as to where to put the collection that contains LabelValueBean objects.
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