Apache Jakarta Struts: Multiple Preselected Options

Published: Thursday, 14 October 2004

String array property

As part of the <html:select> element you can have multiple set to true. In the Action Form, the corresponding property must be an array of String, then the options that belong to the select will be automatically preselected when using <html:optionsCollection>.

Sample code below:

<html:select multiple="true" property="roles">
  <html:optionsCollection name="roles" label="name" value="id"/>
</html:select>

As part of the html:select, roles is a the String[] property of the Action Form for this page.

The optionsCollections refers to another bean altogether, called roles, which is a List of objects that have a getName and getId method.

You can also use the <html:multibox> which will render multiple checkboxes. They will be checked or unchecked depending on the values stored in the roles property of your Action Form.

<c:forEach var="role" items="${roles}">
  <html:multibox property="roles">
    <c:out value="${role.id}"/>
  </html:multibox>
  <c:out value="${role.name}"/>
</c:forEach>

Links