Creating a Dynamically Populated ListThis 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. PageHTMLTo 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. ShoppingList.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Shopping List</title> </head> <body> <p>Be sure to pick up the following items on the way home:</p> <ul> <li class="dinner">Wine</li> <li class="Discard">Cheese</li> </ul> <p>If you'd like dessert, please also buy: <ul> <li class="dessert">Cake</li> <li class="Discard">Ice Cream</li> </ul> <p>And don't forget: <ul> <li class="reminder">Gas</li> </ul> <p> But please don't buy any more: <ul> <li class="nono">Jelly Beans <li class="Discard">Tomato Paste </ul> </body> </html> Java classTo make it easier to see what's going on, we've defined the Java elements in the Dynamator file. Dynamator FileTo 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. ShoppingList.dyn
<dynamator language="jsp"> <prolog> <%@ 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[] { "quiche", "salad" }; private static final Vector dessert_ = new Vector(); static { dessert_.addElement("Pie"); dessert_.addElement("Ice Cream"); } private static final Vector reminder_ = new Vector(); static { reminder_.addElement("Flowers"); } private Enumeration reminder() { return reminder_.elements(); } %> <% List nonos = new LinkedList(); nonos.add("Day-old sushi"); nonos.add("Fruitcake"); %> </prolog> <class name="dinner"> <foreach type="String[]" element="menuItem"> dinner_ </foreach> <content>menuItem</content> </class> <class name="dessert"> <foreach type="Vector[String]" element="dessertItem"> dessert_ </foreach> <content>dessertItem</content> </class> <class name="reminder"> <foreach type="Enumeration[String]" element="reminderItem"> reminder() </foreach> <content>reminderItem + "!"</content> </class> <class name="nono"> <foreach type="Iterator[String]" element="nonoItem"> nonos.listIterator() </foreach> <content>nonoItem</content> </class> </dynamator> 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:
collection-type[collection-element-type]
JSP fileAfter 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 ActionIf you are viewing this page in a servlet engine, you can see the generated page in action. |
||||||
Page last updated 01 April 2004 |
Copyright 2001-2004 by Jay Dunning. All rights reserved. |
hosted by |