This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Prepopulating Radio buttons in JSP

 
Scott Updike
Ranch Hand
Posts: 92
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
Chakravarthy Gogineni
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it working. Thanks to everyone for the examples.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic