tusar
tusar

Reputation: 3424

how to make a <html:multibox> checked by default

I'm new to Struts 1 so may be its already a resolved question. The situation is: I have a list of <html:multibox> tag, which are rendered into html-checkbox element when the page loads. I want the checkboxes to be checked by default (without using javascript/jquery).

Upvotes: 1

Views: 6495

Answers (2)

user844942
user844942

Reputation: 143

The best way to do this is with a *formname*SetupAction.java class.

Set your struts-config.xml to redirect people who click on your page to this SetupAction. Import your form class, populate your String[] with whatever values you want default-checked, and action-forward them back to your page. This also allows you to dynamically populate them, based on DB data or session variables or whatever you want.

Upvotes: 1

Joseph Erickson
Joseph Erickson

Reputation: 2294

You would set the fields in your Form if you want them selected. For multiple checkboxes with all the same name but different values, your Form should have a String[] property that holds all the selected values. Just populate that with the values you want selected by default. This could be something as simple as:

public void reset(ActionMapping mapping, HttpServletRequest request) {
    if(multiboxField == null) {
        multiboxField = new String[2];
        multiboxField[0] = "optionOne";
        multiboxField[1] = "optionTwo";
    }
}

Upvotes: 3

Related Questions