Struts Tip #16 - How to (not) check a radio button

Ted Husted



Each radio button element requires an assigned value that distinguishes it from the other radio buttons. When creating a static array of radio buttons, you need to indicate which one of these, if any, is checked. This does not need to be done when the radio buttons are being populated from dynamic data. The control can compare itself to the form bean's property and then check itself when appropriate.

Given a set of <html:radio> controls like this:

<html:radio property="expectedVia" value="UPS"/>UPS 
<html:radio property="expectedVia" value="FEDX"/>Federal Express 
<html:radio property="expectedVia" value="AIRB"/>AirBorne

And that the expectedVia  property on the form bean was already set to "UPS", then the HTML radio elements would be rendered like this:

<input type="radio" name="expectedVia" value="UPS" checked="checked">UPS 
<input type="radio" name="expectedVia" value="FEDX">Federal Express 
<input type="radio" name="expectedVia" value="AIRB" >AirBorne

If you need to create a dynamic set of radio buttons, or want to localize the values, you can create in an Action a collection of LabelValueBeans with the appropriate labels and values for each button. Here's an example:

ArrayList shippers = new ArrayList(); 
  shippers.add(new LabelValueBean("UPS", "UPS")); 
  shippers.add(new LabelValueBean("Federal Express", "FEDX")); 
  shippers.add(new LabelValueBean("AirBorne", "AIRB")); 
request.setAttribute ("SHIPPERS",shippers);

Then, on the page, you can iterate through the collection

<logic:iterate id="row" name="SHIPPERS" type="org.apache.commons.scaffold.util.LabelValueBean"> 
  <html:radio property="expectedVia" value="<%=row.getValue()%>"/> 
  <bean:write name="row" property="label"/> 

So long as one of the values matches the "expectedVia" property on our ActionForm, the radio tag will still automatically select the appropriate button.

In Struts 1.1, you can use the built-in LabelValue class instead [org.apache.struts.util.LabelValueBean].

Struts 1.1 (beta 2 or later) also provides an additional property to the RadioButton tag that eliminates the need to use a scriplet. With 1.1, we can interate through the same collection this way:

<logic:iterate id="row" name="SHIPPERS"> 
  <html:radio property="expectedVia" value="value" idName="row"/> 
  <bean:write name="row" property="label"/> 

This tells the tag to look for a bean named row, and call its getValue method, which is what the scriplet did.

HTH, Ted.

Say, why does Struts output "checked=checked" rather than something like "checked=true" or just "checked"? To retain XHMTL compatiblity, Struts outputs all properties in a "property=value" format. When doing this for properties that can be present or not (checked, nowrap, et al), the HTML 4.01 specification says to use the same token for the property and the value. So that's what Struts does =:0) For full XHMTL compatability, set xhtml="true" in the Struts <html:html> tag.


Struts Tips  are based on excerpts from the book Struts in Action. The tips released weekly on the MVC-Programmers List. To subscribe, visit BaseBean Engineering.

About Ted. Ted Husted is an active Struts Committer and co-author of Struts in Action and Professional JSP Site Design. Ted also manages the JGuru Struts FAQ and moderates the Struts mailing list.

Copyright Ted Husted 2002. All rights reserved.