Previous examples show how to pass an array of String values to the ForEach servlet bean and display elements of that array. This section shows how to pass an array of Nucleus components into a ForEach servlet bean, then use the servlet bean to display a specific property of each component.

For example, the Student components used in earlier examples might be complemented by Dormitory components, which contain two properties:

The Dormitory class instantiated by dormitory components looks like this:

public class Dormitory {
  String name;
  Student[] students;

  public Dormitory () {}
  public void setName (String name) { = name; }
  public String getName () { return name; }
  public void setStudents(Student[] students)  { this.students = students; }
  public Student[] getStudents()  { return students; }

In the following example, the ForEach servlet bean iterates through an array of Student components that are in a Dormitory component, and displays each Student component’s name property:

<dsp:droplet name="/atg/dynamo/droplet/ForEach">
  <dsp:param name="array" bean="/dorms/Markley.students"/>
  <dsp:param name="sortProperties" value="+age"/>
  <dsp:oparam name="outputStart">
    <p>The following students live in
         <dsp:valueof bean="/dorms/"/>:<ul>
  <dsp:oparam name="output">
    <li><dsp:valueof param=""/>&nbsp;&nbsp;
         dsp:valueof param="element.age"/> </li>
  <dsp:oparam name="outputEnd">

In this example, the array input parameter points to Markley.students, an array of Student components. As the ForEach servlet bean iterates through this array, each element output parameter points to the next Student component; the dsp:valueof tag displays the name property of each Student component.

Sorting Elements on Properties

This example uses the input parameter sortProperties, which specifies to sort elements on the Student component’s age property, in ascending order.

In the compiled JSP, the ForEach servlet generates output like this:

<p>These students live in Markley Hall:
<li>Susan Williams  19</li>
<li>Frank Thomas  20</li>
<li>Dwayne Hickman  23</li>

Note: If servlet beans are nested, an inner servlet beans inherits from the outer servlet beans the most proximate sortProperties setting, unless it has its own sortProperties setting.

Copyright © 1997, 2014 Oracle and/or its affiliates. All rights reserved. Legal Notices