Dynamator Pure HTML for every page generation technology.

Creating a Dynamically Populated List

This example shows how to output the elements of each JDK sequenced collection supported by Dynamator (array, Vector, Enumeration, Iterator). In this example, the list is a shopping list.



To cause the content of the 'li' element to be subject to change at run-time, add a 'class' attribute to the 'li' element.

When page displays a list of items, more than one item is usually required to present sample data. The HTML page can be used to display sample data that is not intended for inclusion in the production system by marking each HTML element to be excluded with the attribute 'class="Discard"'. When Dynamator detects a element with a class or id with the identifier "Discard", it does not copy the element or its children to the JSP page. The effect is the same as using the element <discard/> in the Dynamator file. The difference is in who can specify the attribute.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <title>Shopping List</title>
    <p>Be sure to pick up the following items on the way home:</p>
      <li class="dinner">Wine</li>
      <li class="Discard">Cheese</li>
    <p>If you'd like dessert, please also buy:
      <li class="dessert">Cake</li>
      <li class="Discard">Ice Cream</li>
    <p>And don't forget:
      <li class="reminder">Gas</li>
    But please don't buy any more:
      <li class="nono">Jelly Beans
      <li class="Discard">Tomato Paste

Java class

To make it easier to see what's going on, we've defined the Java elements in the Dynamator file.

Dynamator File

To indicate that an HTML element should be repeated for each element of a collection, add a 'foreach' element to the 'class' element in the Dynamator file.

<dynamator language="jsp">
    <%@ page session="false" %>
    <%@ page import="java.util.Enumeration" %>
    <%@ page import="java.util.Vector" %>
    <%@ page import="java.util.LinkedList" %>
    <%@ page import="java.util.List" %>
    <%@ page import="java.util.Iterator" %>
        private static final String[] dinner_ = 
            new String[]
        private static final Vector dessert_ = new Vector();
            dessert_.addElement("Ice Cream");
        private static final Vector reminder_ = new Vector();

        private Enumeration reminder()
            return reminder_.elements();
        List nonos = new LinkedList();
        nonos.add("Day-old sushi");
  <class name="dinner">
    <foreach type="String[]" element="menuItem">
  <class name="dessert">
    <foreach type="Vector[String]" element="dessertItem">
  <class name="reminder">
    <foreach type="Enumeration[String]" element="reminderItem">
    <content>reminderItem + "!"</content>
  <class name="nono">
    <foreach type="Iterator[String]" element="nonoItem">

The Dynamator file contains a 'foreach' element for each repeating element in the HTML file. In each case, the 'foreach' element contains an attribute to specify the type of the collection, and an attribute to specify the name of the Java variable that references the current element of the iteration. So, for example, the first 'foreach' iterates over the array of Strings referred to by variable 'dinner_', giving the variable name 'menuItem' to the current element.

The content of the 'foreach' element is a Java expression that evaluates to a collection of the same type as specified in the 'type' attribute. In the example, the first two foreach elements reference static collection variables, the third case references a local method call that returns a collection, and the last case references a method call on a local variable.

As elsewhere, the 'content' element within the 'class' element causes the content of the referenced element to be replaced with the content of the 'content' element. In each of these cases, the content uses the variable referring to the current element of the collection. To show that it doesn't have to be just the element, the reminder content element adds something extra.

The format of the 'type' attribute is consistent across all collection types except for arrays:


JSP file

After processing with Dynamator, the resulting JSP file looks like this:

ShoppingList.jsp (generated by Dynamator)

Dynamator generates variable names to support iteration. Generated names are based on the foreach expression. You can specify your own variable names; see the documentation for <foreach>. The dollar signs are valid Java, but are usually only used by code generators, which is what Dynamator is. They are used to ensure that generated names are readable but don't conflict with any programmer-defined names.

In Action

If you are viewing this page in a servlet engine, you can see the generated page in action.