how to make a <html:multibox> checked by default
I'm new to Struts 1 so may be its开发者_JAVA百科 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).
You would set the fields in your Form
if you want them selected. For multiple checkboxes with all the same name
but different value
s, 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";
}
}
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.
精彩评论