Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

preselect a radio button

 
Christian Nash
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
dnyan ginde
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
dnyan ginde
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the correction.
 
Christian Nash
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to do this dynamically then you can have the values of your radio buttons mapped to a bean.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic