3.11. Generating a Set of Related Radio Buttons

Problem

You want to generate a set of related radio buttons whose values are dynamically based on values retrieved from a Collection.

Solution

Expose the set of values for the radio buttons as a Collection that can be iterated over using the logic:iterate tag. The idName attribute of the html:radio tag should be the same as the value of the id attribute of the iterate tag. Use the value attribute of the html:radio tag to specify a property of the idName object. The value of this property will be the value for the generated input type="radio" HTML control:

<logic:iterate id="loopVar" name="MyForm" property="values">
  <html:radio property="beanValue" idName="loopVar" value="value"/>
  &nbsp;
  <bean:write name="loopVar" property="name"/>
  <br />
</logic:iterate>

Discussion

Radio buttons are HTML controls in which one button can be selected at a time. Radio buttons are grouped together based on the name attribute of the HTML input tag. Like other HTML form input elements, the label for the control isn't part of the control itself. Developers label the control however they want using regular text. Typically, radio buttons are labeled with the text to the right of the input tag:

<input type="radio" name="skill" value="1"/> Beginner <br />  
<input type="radio" name="skill" value="2"/> Intermediate <br />  
<input type="radio" name="skill" value="3"/> Advanced <br />

In some cases, the set of radio buttons in a group is dynamic. In other words, the radio ...

Get Jakarta Struts Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.