Developing an Iterating Action

As I alluded to earlier, a tag handler can iterate over the element’s body until some condition is true. The evaluation of the body may be different for each iteration, since the tag handler can introduce a variable (used in the body) that changes its value. An example of an iterating action is the <ora:loop> used in this book. It can be used to iterate over the element body once for each value in an array, a java.util.Vector, a java.util.Dictionary, or a java.util.Enumeration. Here’s an example of how the <ora:loop> action can be used:

<%@ page language="java" contentType="text/html" %> 
<%@ taglib uri="/orataglib" prefix="ora" %>
<html>
  <body bgcolor="white">
    <%
      String[] test = new String[4];
      test[0] = "first";
      test[1] = "second";
      test[2] = "third";
      test[3] = "fourth";
      pageContext.setAttribute("test", test);
    %>

    <pre>
      <ora:loop name="test" loopId="x" className="java.lang.String">
        Current value: <%= x %>
      </ora:loop>
    </pre>
  </body>
</html>

Here, the <ora:loop> tag iterates over the elements of a String array, adding the current value to the response using a JSP expression in the action’s body.

The com.ora.jsp.tags.generic.LoopTag class is the tag handler class for the <ora:loop> action. It extends BodyTag support and has four properties:

public class LoopTag extends BodyTagSupport {
    private String name;
    private String loopId;
    private String className;
    private String property;
    ...

A standard property setter method is provided for each property. This ...

Get Java Server Pages now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.