Wednesday, February 20, 2008

Usage of logic:iterate Tag in Struts JSP!

Iterate - Repeat the nested body content of this tag over a specified collection.

Repeats the nested body content of this tag once for every element of the specified collection,

which must be an , a , a (whose values are to be iterated over), or an array.

The collection to be iterated over must be specified in one of the following ways:

As a runtime expression specified as the value of the attribute.
As a JSP bean specified by the attribute.
As the property, specified by the , of the JSP bean specified by the attribute.

The collection to be iterated over MUST conform to one of the following requirements in order for iteration to be

successful:

An array of Java objects or primitives.
An implementation of java.util.collection, including ArrayList and Vector.
An implementation of java.util.Enumeration
An implementation of java.util.Iterator
An implementation of java.util.map, including Hashmap,HashTable , and Treemap.

- See below for additional information about accessing Maps.

Normally, each object exposed by the iterate tag is an element of the underlying collection you are iterating over.

However, if you iterate over a , the exposed object is of type that has two properties:

- The key under which this item is stored in the underlying Map.
- The value that corresponds to this key.

So, if you wish to iterate over the values of a Hashtable, you would implement code like the following:

<logic:iterate id="element" name="myhashtable">
Next element is <bean:write name="element" property="value"/>
</logic:iterate>

If the collection you are iterating over can contain values, the loop will still be performed but no page scope attribute

(named by the attribute) will be created for that loop iteration. You can use the and tags to test for this case.

Attributes:

<logic:iterate collection>
A runtime expression that evaluates to a collection (conforming to the requirements listed above) to be iterated over.

<logic:iterate id>
The name of a page scope JSP bean that will contain the current element of the collection on each iteration, if it is not .


<logic:iterate indexId>
The name of a page scope JSP bean that will contain the current index of the collection on each iteration.

<logic:iterate length>
The maximum number of entries (from the underlying collection) to be iterated through on this page. This can be either
an integer that directly expresses the desired value, or the name of a JSP bean (in any scope) of type that defines the desired value.
If not present, there will be no limit on the number of iterations performed.

<logic:iterate name>
The name of the JSP bean containing the collection to be iterated (if is not specified), or the JSP bean whose property
getter returns the collection to be iterated (if is specified).

<logic:iterate offset>
The zero-relative index of the starting point at which entries from the underlying collection will be iterated through.
This can be either an integer that directly expresses the desired value, or the name of a JSP bean (in any scope) of type
that defines the desired value. If not present, zero is assumed (meaning that the collection will be iterated from the beginning.

<logic:iterate property>
Name of the property, of the JSP bean specified by , whose getter returns the collection to be iterated.

<logic:iterate scope>
The bean scope within which to search for the bean named by the property, or "any scope" if not specified.



No comments: