|
Programmers Guide > JOT Iterators
JOT Iterators
JotTemplateIterators render dynamic tables and lists using
Java Collections and JDBC ResultSets. JOT Iterators
are easily adapted for use with any other group of objects
(see
JotIteratorSource).
The JOT.Repeat template
token starts a loop that repeats a section of a JOT Template
up to a matching JOT.End token. The looping continues while the argument
to JOT.Repeat is true. This boolean argument is typically a JOT.Next
token that refers to a JotTemplateIterator. In the following example,
Items is a name for a JotTemplateIterator. Items.ProductName
and Items.ProductPrice take on values from the current element
in the iteration loop.
JOT.Repeat(JOT.Items.Next)
<tr>
<td>JOT.Items.ProductName</td>
<td>JOT.Items.ProductPrice</td>
</tr>
JOT.End
A useful design idiom for JOT Iterators is an inner class that extends
JotTemplateIteratorBean,
JotCollectionIteratorBean, or
JotMapIteratorBean. The next code fragment continues
the example with an inner Items class that enumerates the
elements of a Vector of Products. Each pass of the JOT.Repeat(JOT.Items.Next)
loop advances the iterator to the next Product object
in the Enumeration which is returned by getElement().
// Fragment of a JotServletPropertyBean...
{
Vector products = new Vector();
products.addElement(new Product("Basic Widget",199.99));
products.addElement(new Product("Super Widget",123.45));
products.addElement(new Product("Broken Widget",0));
}
public void prepareJotBean()
throws JotException
{
super.prepareJotBean();
addJotBeanProperty("Items", new Items()); // create property
}
// Inner class
public class Items
extends JotTemplateIteratorBean
{
Items()
{
super(new JotEnumIterator(products.elements()));
}
public String getProductName()
{
return ((Product)getElement()).productName;
}
public String getProductPrice()
{
return fmt.format(((Product)getElement()).price);
}
}
The properties of the current element in an iteration are
automatic properties of the JotTemplateIterator.
The previous example can be refactored to eliminate the Items
subclass if getProductName() and getProductPrice() are
methods of the Product class, as shown here with a
JotCollectionIteratorBean.
// Fragment of a JotServletPropertyBean...
public void prepareJotBean()
throws JotException
{
super.prepareJotBean();
// create "Items" JOT Iterator property
addJotBeanProperty("Items", new JotCollectionIteratorBean(products));
}
The JotTemplateIterator
element property references the current object in an iteration.
The
position property renders the ordinal position (element count)
of the current object. In this example the Product object is
passed to the getSalesPrice() method:
JOT.Repeat(JOT.Items.Next)
<tr>
<td>JOT.Items.Position</td>
<td>JOT.Items.ProductName</td>
<td>JOT.Items.ProductPrice</td>
<td>JOT.SalesPrice(JOT.Items.Element)</td>
</tr>
JOT.End
The JOT Servlets documentation provides further JOT Iterator
examples showing how to limit the number of iterations
and how to use JOT Iterators with JDBC ResultSets.
|