Using The Struts Multibox Tag With LabelValueBeans

The Jakarta Struts <html:multibox> tag can be used to display a group of checkboxes on an HTML form. The tag sets the state of the checkboxes according to the contents of an associated array. The LabelValueBean is a simple class that associates a label with a value. This can be used in combination with the <html:multibox> tag to create checkboxes that return different strings to the ones displayed in the user interface.

The code given below displays four checkboxes with the labels “Alpha”, “Beta”, “Charlie” and “Delta” and the checkboxes return the values “A”, “B”, “C” and “D” respectively.

The JSP:

<logic:iterate name=”myActionForm” id=”item” property=”possibleOptions”>
<html:multibox property=”selectedOptions”>
<bean:write name=”item” property=”value” />
</html:multibox>
<bean:write name=”item” property=”label” /><br />
</logic:iterate>

The Struts Form class:

import org.apache.struts.util.LabelValueBean;

public class MyActionForm extends ActionForm {
private LabelValueBean[] possibleOptions;
private String[] selectedOptions;

public MyActionForm() {
// Initialise the LabelValueBeans in the possibleOptions array.
LabelValueBean[] lvBeans = new LabelValueBean[4];

lvBeans[0] = new LabelValueBean(“Alpha”, “A”);
lvBeans[1] = new LabelValueBean(“Beta”, “B”);
lvBeans[2] = new LabelValueBean(“Charlie”, “C”);
lvBeans[3] = new LabelValueBean(“Delta”, “D”);

this.possibleOptions = lvBeans;
}

public LabelValueBean[] getPossibleOptions() {
return possibleOptions;
}

public String[] getSelectedOptions() {
return selectedOptions;
}

public void setSelectedOptions(String[] selectedOptions) {
this.selectedOptions = selectedOptions;
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.