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

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}"/>.

Previous
Next