File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes preselect a radio button 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 "preselect a radio button" Watch "preselect a radio button" New topic
Author

preselect a radio button

Christian Nash
Ranch Hand

Joined: Jan 17, 2006
Posts: 107
Hi all,

I would like to know as to how i can preselect the topmost radio button from a list of buttons in Struts using the struts tags.

Cheers,
Ashwin


- Christian
dnyan ginde
Ranch Hand

Joined: Jan 17, 2006
Posts: 68
hi, hope this solves ur problem....

say you have the following radio buttons


Its possible to pre-select any radio button by specifying a default value for the property on the actioon form associated with the radio button. The property setter method must be called from the constructor and the reset method. Note that it should not be called from the reset method if the bean is in session scope because the selections will be reset for each page.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Even if the form is in session scope, setting the value in the reset() method should be OK since Struts calls the reset() method prior to calling the setters on the bean. The reset() method will set it to the default value, and then setSelected() will set it to the value entered by the user.

If you don't want to set default values in the ActionForm, you can just set the value in the Action class that forwards to the JSP.


Merrill
Consultant, Sima Solutions
dnyan ginde
Ranch Hand

Joined: Jan 17, 2006
Posts: 68
Thanks for the correction.
Christian Nash
Ranch Hand

Joined: Jan 17, 2006
Posts: 107
hi,

But i will not like to hard code the values either in the Action form or the jsp page. Is there a wy to do this preselection dynamically

your code said that
code:
--------------------------------------------------------------------------------

<html:radio property="selected" value="One">One</html:radio><html:radio property="selected" value="Two">Two</html:radio><html:radio property="selected" value="Three">Three</html:radio>

--------------------------------------------------------------------------------




public class MyActionForm extends ActionForm{ private String selected; public MyActionForm() { setSelected("One"); } public void reset( ActionMapping mapping, HttpServletRequest request ) { setSelected("One"); } public String getSelected() { return this.selected; } public void setSelected( String selected ) { this.selected = selected; }}

Thanks and Cheers,
Ashwin
dnyan ginde
Ranch Hand

Joined: Jan 17, 2006
Posts: 68
If you want to do this dynamically then you can have the values of your radio buttons mapped to a bean.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: preselect a radio button
 
Similar Threads
Radio buttons
how to display a textbox when a radio button is selected
how to enable radio button
struts and ajax
Handling of radio buttons