Struts 1 How to set checkbox default checked

15,609

Solution 1

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"

Solution 2

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.

Share:
15,609

Related videos on Youtube

hendry.fu
Author by

hendry.fu

Updated on June 04, 2022

Comments

  • hendry.fu
    hendry.fu almost 2 years

    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
    hendry.fu over 12 years
    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
  • Dave Newton
    Dave Newton over 12 years
    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
    Dave Newton almost 9 years
    It's helpful to comment on downvotes. The reset method is the correct place to set default checkbox values.