1

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).

tusar
  • 3,364
  • 6
  • 37
  • 60

2 Answers2

3

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";
    }
}
Joseph Erickson
  • 2,304
  • 20
  • 29
  • Thanks @Joeshph ! I tried exactly this code. It works, only when the "multiboxField" is an array of boolean, and I set them to "true". And also in jsp I have to set value="true" in the field. And this generally fulfills my need, please revert if any wrong concept I'm applying here. – tusar Aug 29 '11 at 07:27
1

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.

user844942
  • 133
  • 1
  • 5
  • Thanks ! Exactly what I'm following... may be there are some configuration problem with my application. Value of the multibox does not get set. – tusar Aug 29 '11 at 07:21