The radio tag is rendered as an <input type="radio"> element. You use a series of radio tags to present options from which only one option can be selected. The radio tag is similar to the multibox tag, except that the multibox tag allows the selection of more than one option.
The attributes that can be used with the radio tag are given in Table 3.10.
Note
You can use a select tag in place of a series of radio tag.
As an example, the following snippet
<html:radio property="userType" value="1"/>Individual <html:radio property="userType" value="2"/>Organization
is translated into:
<input type="radio" name="userType" value="1">Individual <input type="radio" name="userType" value="2">Organization
| Attribute | Description | Possible Value(s) |
|---|---|---|
| idName | Specifies the scoped variable exposed by an iterator, if an iterator is used to automatically generate a series of radio tags. | string |
| indexed | Indicates whether the value assigned to the name attribute is indexed. | true or false |
| name | The scoped object containing the property specified by the property attribute. If the name attribute is not present, the value of the name attribute of the enclosing form tag will be used. | string |
| property* | The property of the action form mapped to the enclosing form tag that will be associated with the rendered HTML input field, unless overridden by the value attribute. | string |
| value* | A constant that will become the value of the rendered radio button. | string |
Table 3.10: The radio tag’s attributes
You can also use the forEach tag to retrieve a collection of label/value pairs. Suppose you have an incomeLevelOptions property in an action form, such as this.
public ArrayList getIncomeLevelOptions() {
ArrayList options = new ArrayList();
options.add(new LabelValueBean("0 - $10,000", "1"));
options.add(new LabelValueBean("$10,001 - $30,000", "2"));
options.add(new LabelValueBean("$30,001 - $50,000", "3"));
options.add(new LabelValueBean("Over $50,000", "4"));
return options;
}
You can use a forEach tag like this.
<c:forEach items="${profileForm.incomeLevelOptions}"
var="incomeLevel">
<html:radio property="cities" idName="incomeLevel" value="value"/>
<c:out value="${incomeLevel.label}"/><br/>
</c:forEach>
The forEach tag’s items attribute specifies the scoped variable and the property name that contains a collection of label/value pairs. The action form is named profileForm, and the property name inside profileForm is incomeLevelOptions. Therefore, the items attribute is given the value ${profileForm.incomeLevelOptions}.
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, incomeLevel). Therefore, from inside the forEach tag, you can reference the incomeLevel scoped variable.
The radio tag’s idName attribute specifies the scoped variable referencing an object whose property will be used. In this case, the scoped variable is incomeLevel, hence the value of the idName attribute. The idName attribute references a LabelValueBean object that has two properties: value and label. The radio tag’s value attribute specifies the name of the property that contains the value. In this case, the property name is value, hence value="value". The label can be found in the label property, hence <c:out value="${incomeLevel.label}"/>.