How to (not) check a radio button

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"/>
</logic:iterate>

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"/>
</logic:iterate>

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

HTH, Ted.

Struts Tips are excerpts from the book Java Web Development with Struts.
<http://husted.com/struts/book.html>

The tips released weekly on the MVC-Programmers List. To subscribe, 
visit BaseBean Engineering. <http://basebeans.com/>

About Ted. Ted Husted is an active Struts Committer and co-author of 
Java Web Development with Struts and Professional JSP Site Design. Ted 
also moderates the Struts mailing list and the JGuru Struts FAQ.

Copyright Ted Husted 2002. All rights reserved.

###

_______________________________________________
MVC-Programmers mailing list
[EMAIL PROTECTED]
http://www.netbean.net/mailman/listinfo/mvc-programmers

Reply via email to