Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General 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 ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

how to make html:radio default selected

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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
ajay
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can initialize the default value in the form bean property
 
Today you are you, that is turer than true. There is no one alive who is youer than you! - Seuss. Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic