• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Prepopulating Radio buttons in JSP

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a struts form where the end user has the option of selecting one of two radio buttons (Yes or No) where both have the same property value. I'm using the struts supplied tags-html tagset where I define my radio button with html:radio tags. I have two questions regarding this tag:

1) How do I default a value to one of the two possible values (ie, no)?
2) The value of the radio button (yes or no) is written to a database. How do I bring that value back and "pre-populate" the radio button based on the value stored in the database. (ie, if No is in the database, how do I populate the radio button so that "No" is selected in the resulting JSP page?
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Action form:



----------------------------------------------------------------
1) In Jsp


-----------------------------------------------------------------
2) You can fill your actionform properties with values in the database through action (or some business object) before forwarding it to jsp.
In that case you need not set property manually on the jsp.
[ March 22, 2007: Message edited by: Vishnu Koya ]
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Scott Updike:
1) How do I default a value to one of the two possible values (ie, no)?
2) The value of the radio button (yes or no) is written to a database. How do I bring that value back and "pre-populate" the radio button based on the value stored in the database. (ie, if No is in the database, how do I populate the radio button so that "No" is selected in the resulting JSP page?


The <html:radio> tag automatically shows the button as checked if it matches the value of the property specified in your ActionForm. I'd suggest that you create a property of type boolean in your ActionForm. If you want the default value to be false, or "no", you don't have to do anything, since the default value of a boolean is always false. If you want the default value to be true, the best way to do it is to override the reset() method of your ActionForm, and set the value to true in that method.

You can then save the value in the database as a boolean, or translate it to a VARCHAR "Y" or "N" as you prefer. When you retrieve it from the database, simply set the appropriate value for the property in the ActionForm, and the correct radio button will be selected when the form is displayed.

Here's what your JSP would look like:

Here's what your ActionForm would look like:
 
Scott Updike
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it working. Thanks to everyone for the examples.
 
Blood pressure normal? What do I change to get "magnificent"? Maybe this tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic