There is no "automatic" way to do this, but I do have a "lost recipe" that didn't make it into the book that I'll post below (please excuse any typos)......
A Simple Radio Button Solution
Problem
How can I create a
JSP page with dynamic radio buttons?
Background
Struts allows for the creation of dynamic radio buttons by nesting the �<logic:iterate/>� and �<html:radio/>� tags referring to an instantiated bean (our example uses the form bean). This nesting works by iterating through a list of radio button values and assigning an identical �name� attribute and the proper value attribute. By using another variable field we can also compare the two lists and assign a predetermined �checked� attribute, if necessary. The big difference between the radio button implementation and the �multibox� implementation in another recipe is how the pre-selected button is created. While it is a fairly simple matter to pre-select checkboxes, the radio button needs a different solution that is readily accomplished using the form bean and a couple of lines of JavaScript!
Recipe
The
Java class formBean does a lot of the heavy lifting in this simple case. The values that you may ultimately wish to populate your application may come from databases, separate beans, DynaActions, EJB�s or combinations of all of these. For purposes of simplicity and clarity, we�ll start with the simple example below:
RadioTestForm.java
package com.strutscookbook;
import ...;
/**
* Creates a
String[] mountains for Radio buttons,
* and a String selectedMountains for the pre-selected
* radio button
*
* @author Danilo Gurovich
*/
public final class RadioTestForm
extends ActionForm {
// Instance Variables
/*Mountains "pre-selected"...*/
private String selectedMountains = "Kangchenjunga";
/*Mountains for radio buttons*/
private String[] mountains = {"Everest","K2","Kangchenjunga","Lhotse",
"Makalu" ,"Cho Oyu"};
/*Getter for selectedMountains*/
public String getSelectedMountains() {
return this.selectedMountains;
}
/*Setter for selectedMountains*/
public void setSelectedMountains(String selectedMountains) {
this.selectedMountains = selectedMountains;
}
/*Getter for the mountains*/
public String[] getMountains() {
return this.mountains;
}
/*Setter for the mountains*/
public void setMountains(String[] mountains) {
this.mountains = mountains;
}
}
All of the java code for this Form Bean (except the usual imports) is included to make sure that there are no discrepancies as to what is needed. Notice that �Kangchenjunga� is listed in both the �selectedMountains� and �mountains� field. We will propagate �Kangchenjunga� to the JSP as the �pre-selected� initial value.
Here is the pertinent JSP Code for the corresponding page. As always, make sure that you import the corresponding struts tags onto your page. Note the correlation between the java files and the �logic�, �html� and �bean� tags, and the JavaScript function located at the bottom of the form:
testForm.jsp
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
...
<html:form action="/FormAction"
name="testForm"
type="com.strutscookbook.CheckboxTestForm">
<h4><bean:message key="testForm.instruction"/></h4>
<!-- gets the "selected" radio button -->
<bean

efine id="selectedRadio" property="selectedMountains" name="testForm"/>
<!-- creates the radio button list -->
<logic:iterate id="mountain" property="mountains" name="testForm">
<bean

efine id="mountainValue"><bean:write name="mountain"/></bean

efine>
<html:radio property="selectedMountains" value="<%=mountainValue%>" styleId="<%=mountainValue%>"/>
<bean:write name="mountain"/><br/>
</logic:iterate>
<br/>
<html:submit/><html:reset/>
<script language="JavaScript">
<!--
var selRadio = document.getElementById("<bean:write name="selectedRadio"/>");
selectedRadio.checked=true;
-->
</script>
</html:form>
Analysis
JavaScript is going to do the work here. First, we define a JSP scripting variable for our �selectedMountain� field above inside the form:
<bean

efine id="selectedRadio" property="selectedMountains" name="testForm"/>
then, we create a JavaScript function below the form. This function consists of two lines:
var selRadio = document.getElementById("<bean:write name="selectedRadio"/>");
selRadio.checked=true;
Here�s what�s happening. We create a �selRadio� JavaScript variable, then find all of the elements in the document that have an �id� (or �styleId� in the pre-compiled code) matching the �selectedRadio� variable. We�ve accomplished this by setting the �<html:radio/>� tag�s �styleId� attribute to match it�s name/value. While the JavaScript function quickly iterates through the id�s on the page, it simply sets our singular radio button as selected.
Another JavaScript method is available to produce the same results, only with a method:
var selectedRadio =
document.forms["testForm"].elements["<bean:writename="selectedRadio"/>"];
selectedRadio.checked=true;
This particular script discriminates to only the form elements �name� instead of �id�. Either System will work perfectly, the dependency exists on extending or scaling of other objects on your page. The output from our JSP looks like:
Everest
K2
Kangchenjunga
Lhotse
Makalu
Cho Oyu