John Topley's Knowledgebase

Using The Struts Multibox Tag With LabelValueBeans

Wednesday, 17 September 2003

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.

Download 0027.txt

  • 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 ActionForm 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;
  }
}

top | index | previous | next | comments ()

home | archive | kb | media | about | contact | accessibility
Copyright © 2003 - 2005 John Topley. Made with CityDesk.