Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

How to make dynamic radio button checked

Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This will help you.
Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
John King
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic