| < Day Day Up > |
|
The <logic:iterate /> tag is used to iterate over a named collection-which contains a Collection, Enumerator, Iterator, Map, or Array-and evaluates its body for each Object in the collection. We can identify the collection being iterated over by using a request-time expression or a scripting variable. The <logic:iterate /> tag has a body type of JSP and supports nine attributes, described in Table 18.8.
Attribute | Description |
---|---|
id | Specifies a JSP scripting variable, exposed by the <logic:iterate /> tag, that will hold the current object in the named collection. (Required) |
collection | Used to identify a collection using a request-time expression. (Optional) |
name | Specifies a scripting variable that represents the collection to be iterated over. (Optional) |
property | Specifies the data member of the scripting variable, identified by the name attribute, that contains a reference to a collection. (Optional) |
scope | Defines the scope of the bean specified by the name attribute. If the scope attribute is not specified, then the tag will search for the bean in the scopes, in the order of page, request, session, and application. (Optional) |
type | Provides the fully qualified class name of the element being exposed from the collection. This object is referenced by the id attribute. (Optional) |
indexId | Specifies a JSP scripting variable, exposed by the <logic:iterate /> tag, that will hold the current index of the current object in the named collection. (Optional) |
length | Identifies the maximum number of collection entries to be iterated over. The length attribute can be either an integer or a scripting variable of type java.lang.Integer. If the length attribute is not included, then the entire collection will be iterated over. (Optional) |
offset | Indicates where iteration should begin. If this value is not specified, then the beginning of the collection is used. (Optional) |
An example of using the <logic:iterate /> tag is shown here:
<logic:iterate name="employees"> <tr align="left"> <td> <bean:write name="employee" property="username" /> </td> <td> <bean:write name="employee" property="name" /> </td> <td> <bean:write name="employee" property="phone" /> </td> </tr> </logic:iterate>
In this example, we are iterating over the collection referenced by the employee's scripting variable. As the <logic:iterate /> tag iterates over the named collection, it exposes each object in the collection in the employee scripting variable. The result of this iteration is an HTML table row for each object in the named collection.
| < Day Day Up > |
|