Apache Jakarta Struts Multiple Preselected Options

String array property

As part of the <html:select> element you can have multiple set to true. The corresponding Form property should then be an array of String. As part of the Action Form, the corresponding property must be an array of String, then the options that belong to the select will automatically preselecting themselves, 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>
  

Published: Thursday, 14 October 2004