This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to make dynamic radio button checked" Watch "How to make dynamic radio button checked" New topic
Author

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

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
http://www.jguru.com/faq/view.jsp?EID=1028442

This will help you.


SCJA,SCJP,SCWCD,SCBCD,SCEA I
Java Developer, Thailand
Carol Enderlin
drifter
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:
http://www.jguru.com/faq/view.jsp?EID=1028442

This will help you.
Danilo Gurovich
author
Greenhorn

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

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


Danilo Gurovich<br />Manager, Web Development<br /> <br />dgurovich@lowermybills.com<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
Greenhorn

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: http://aspose.com/file-tools
 
subject: How to make dynamic radio button checked
 
Similar Threads
To iterate HashMap in struts
html:radio
html radio
selecting a radio button for IE & Netscape
Using html:radio with indexed attribute