Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to set checkbox checked by default

 
amit sharma
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to set checkbox by default .I search net and some people give advice to set set the value true in reset method but the problem with it is that when user uncheck the checkbox then too i receive the value true .
Thanks
 
K Dombroski
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Checkboxes in Struts 1 are a pain because they don't get passed through the request when they are not checked, so you have to make special allowances to uncheck them. Therefore, I use radio buttons whenever possible. However, sometimes I need a checkbox, so what I usually do is set the default value in my action class before loading the page (or sometimes directly in the form, as in 'String myCB = "true"'), then have a hidden input in my jsp as follows:

<html:checkbox property="myCB" value="true"/>
<html:hidden property="myCB" value="false"/>

That usually does the trick for me. Hope this helps.
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I normally do is set the property to true in the action class that forwards to the JSP if I want it checkec by default, and then I set it to false in the reset() method of the ActionForm bean. That way if the user unchecks it, it will get cleared properly.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic