File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes How to set checkbox checked by default Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to set checkbox checked by default" Watch "How to set checkbox checked by default" New topic

How to set checkbox checked by default

amit sharma
Ranch Hand

Joined: Jul 19, 2006
Posts: 129
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 .
K Dombroski
Ranch Hand

Joined: Dec 03, 2005
Posts: 39
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

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

Consultant, Sima Solutions
I agree. Here's the link:
subject: How to set checkbox checked by default
It's not a secret anymore!