3

I'm making some small changes to an existing project, which was built using Struts 1.2. I need to have a check box on the form checked by default. I'm a newbie on Struts.

I understand that I can't set a default value for check boxes on form reset method, in fact it is suggested to set the value to false for checkboxes in reset method. The only way I can think of is to check the POST param, but I don't think this is a good solution.

Is there a correct Struts way to have a default checked checkbox?

hendry.fu
  • 309
  • 1
  • 3
  • 9

3 Answers3

2

you have:

<html:checkbox property="multiRole" value="Y" />

if you want the page rendered with it checked by default, you need to set the property "multiRole" in your action form to "Y"

francesco.s
  • 306
  • 1
  • 10
1

Why can't you set a default value in reset? That's mostly what it's for. You can also set a value in the action that initially displays the form.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • 4
    because if I set the value to `On` in reset method and user unchecked it, the value will not be reflected in ActionForm, so it will always be _checked_ - you can refer to here: [struts-taglib doc](http://struts.apache.org/1.x/struts-taglib/tlddoc/html/checkbox.html) – hendry.fu Sep 27 '11 at 11:01
  • Obviously you'd set it to the /correct/ value, the determination of which depends on how everything else is set up. You said you wanted a default checked checkbox, but that's not actually what you want. – Dave Newton Sep 27 '11 at 11:10
  • 1
    It's helpful to comment on downvotes. The `reset` method *is* the correct place to set default checkbox values. – Dave Newton May 26 '15 at 21:25
0

alternatively you can use the ,,normal" html tag to solve this problem. This tag can also interact with the form bean via the name parameter. In the form bean you can use a boolean parameter!

<input type="checkbox" name="multiRole" checked="checked" />