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 html:radio default selected Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "how to make html:radio default selected" Watch "how to make html:radio default selected" New topic

how to make html:radio default selected

Ajay Tickle

Joined: Mar 16, 2008
Posts: 17

I have something like this in JSP :

<html:radio property="gender" value="male">MALE</html:radio>
<html:radio property="gender" value="female">FEMALE</html:radio>

1) i want to know how to make "Male" to be default selected when page is loaded
2) I would set the value in form bean and on second page i would display radio buttons to change the value. i would like to know how to make one radio button selected as per the value set in form bean

thank you
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
The problem I see is with the way you've written your tag. If you specify a value attribute you should not specify a body and visa-versa. It's confusing because the value in the body is different that what is in the value attrubute. Change your tags to:

<html:radio property="gender" value="male" />Male
<html:radio property="gender" value="female" />Female

Then just set the ActionForm property gender to "male" sometime prior to displaying the page, and the male button will show as selected.

Consultant, Sima Solutions
Alessandro Valcepina

Joined: May 09, 2008
Posts: 1
I have a similar problem, but the solution you suggested is unavailable to me since i need to set the radio to the value of a request attribute (a field of an object that gets passed as a request attribute to be more precise).
How can I do that?
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
You could put the following tag in your JSP before the radio buttons:

This works, but the problem is that if the page is redisplayed because of a validation error, the button reverts to this value, rather than the value the user actually selected.

A better solution would be to have whatever process is setting the value in the request scope set it in the ActionForm bean instead.
Amirtharaj Chinnaraj
Ranch Hand

Joined: Sep 28, 2006
Posts: 241
you can initialize the default value in the form bean property
I agree. Here's the link:
subject: how to make html:radio default selected
It's not a secret anymore!