*
The moose likes Struts and the fly likes Prepopulating Radio buttons in JSP 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 "Prepopulating Radio buttons in JSP" Watch "Prepopulating Radio buttons in JSP" New topic
Author

Prepopulating Radio buttons in JSP

Scott Updike
Ranch Hand

Joined: Feb 16, 2006
Posts: 92
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?
Chakravarthy Gogineni
Greenhorn

Joined: Nov 09, 2005
Posts: 10
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 ]

SCJP, SCWCD
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
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:


Merrill
Consultant, Sima Solutions
Scott Updike
Ranch Hand

Joined: Feb 16, 2006
Posts: 92
I got it working. Thanks to everyone for the examples.
 
 
subject: Prepopulating Radio buttons in JSP
 
Similar Threads
Problem in Struts radio button
how to display a textbox when a radio button is selected
validating textfield with +ve/-ve values using 2 radiobuttons
Radio buttons.......
JSP and Radio Buttons