Jakarta > Tapestry
Jakarta
 
Font size:      

Foreach

A component that loops through a set of values, setting a property for each value before rendering its wrapped elements.

See also: For

Warning
This component has been deprecated; Tapestry 4.0 adds the For component which replaces Foreach and ListEdit components.

Parameters

Name Type Direction Required Default Description
source Iterator, Collection, Object[], or Object in yes The source of objects to be iterated, which may be a Collection, an Iterator, an array of Objects, or a even a single Object (which is treated as a singleton collection). The source parameter may even be null, in which case the Foreach's body is never renderred.
value Object out no Used to update the current value on each iteration.
index int out no Used to store the index of the current value within the stream of elements provided by the source parameter. The index parameter is explicitly updated before the value parameter.
element String in no If specified, then the component acts like an Any, emitting an open and close tag before and after each iteration. Most often, the element is "tr" when the Foreach is part of an HTML table. Any informal parameters are applied to the tag. If no element is specified, informal parameters are ignored.

Body: allowed

Informal parameters: allowed

Reserved parameters: none

Examples

The Foreach component is used to generate a table from a Customer List.

Template

<table cellspacing="6">
  <tr>
   <td>ID</td>
   <td>&nbsp;</td>
   <td>Name</td>
   <td>&nbsp;</td>
   <td>Level</td>
  </tr>
  <tr>
   <td colspan="5"><hr></td>
  </tr>
  <tr jwcid="@Foreach" source="ognl:customerList" value="ognl:customer" element="tr">
   <td><span jwcid="@Insert" value="ognl:customer.id"/></td>
   <td>&nbsp;</td>
   <td><span jwcid="@Insert" value="ognl:customer.fullName"/></td>
   <td>&nbsp;</td>
   <td><span jwcid="@Insert" value="ognl:customer.memberLevel"/></td>
  </tr>
</table>

Page Specification

.....
<property name="customerList" type="java.util.List" persistent="yes"/>
<property name="customer" type="Customer"/>
.....

Java

public abstract class SalesPage extends BasePage {
    public abstract List getCustomerList();

    public abstract void setCustomerList(List value);
}

public class Customer implements Serializable {
    private Integer id;
    private String fullName;
    private String memberLevel;

    public Customer(Integer id, String fullName, String memberLevel) {
        this.id = id;
        this.fullName = fullName;
        this.memberLevel = memberLevel;
    }

    public Integer getId() { return id; }

    public String getFullName() { return fullName; }

    public String getMemberLevel() { return memberLevel; }
}