File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes How to make dynamic radio button checked Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Barcodes with iOS this week in the iOS forum
or Core Java for the Impatient in the Java 8 forum!

JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to make dynamic radio button checked" Watch "How to make dynamic radio button checked" New topic

How to make dynamic radio button checked

John King
Ranch Hand

Joined: Aug 27, 2002
Posts: 165
I�m using <logic:iterate> and <html:radio> to display a set of radio button dynamically:

<logic:iterate id=�student� name=�students� >
<html:radio property=�studentId� value=�id� idName=�student� />

I want the first radio button is always checked initially after the page is loaded.
The id value is not static. How should I do it?
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312

This will help you.

Java Developer, Thailand
Carol Enderlin
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
Somkiat, I don't see what on the jguru URL explains how to make the first radio button checked. Did I miss something?

Originally posted by somkiat puisungnoen:

This will help you.
Danilo Gurovich

Joined: Aug 08, 2002
Posts: 7
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

How can I create a JSP page with dynamic radio buttons?

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!

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:

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:


<%@ 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"

<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/>

<script language="JavaScript">
var selRadio = document.getElementById("<bean:write name="selectedRadio"/>");

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"/>");

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 =

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:

Cho Oyu

Danilo Gurovich<br />Manager, Web Development<br /> <br /><br />2401 Colorado Ave., 2nd Floor <br />Santa Monica, CA 90404<br />(310) 998-6412
John King
Ranch Hand

Joined: Aug 27, 2002
Posts: 165
I thought using FormBean, but I'm using DynaValidatorActionForm to use
the validation framework so there is no FramBean class in my code. Should
I create my own FormBean extends DynaValidatorActionForm? Would it break
JS validations?
g. drogo

Joined: Feb 03, 2005
Posts: 1
I have the same problem (more or less): all my radio controls are checked!
Now I have stepped back and i'm looking at all this matter: it seems really crazy that to insert a 'checked=true' fileld had to be so hard.
Probably there's something wrong or I have miss something.
At this point i will use a java snipplet: it's surely more simple!
I agree. Here's the link:
subject: How to make dynamic radio button checked