Interacting with an Iteration Action

The JSTL specification also defines two interfaces for iteration actions. The javax.servlet.jsp.jstl.core.LoopTag interface must be implemented by iteration actions that want to cooperate with actions nested in their bodies:

public interface LoopTag extends javax.servlet.jsp.tagext.Tag

It defines two methods nested actions can call:

public java.lang.Object getCurrent(  )
public LoopTagStatus getLoopStatus(  )

The getCurrent( ) method returns the current object in the collection the action iterates over. The getLoopStatus( ) returns an object that implements the other JSTL iteration interface: javax.servlet.jsp.jstl.core.LoopTagStatus.

Before we look at the LoopTagStatus interface, let’s see how a custom action can use the LoopTag interface. Example 23-4 shows how such a custom action can be used for the same purpose as the custom iteration action described earlier, namely to generate an HTML checkbox element with the checked attribute set depending on a dynamic list of selections.

Example 23-4. Using a custom action that gets the iteration status from its parent
<form action="foreachoption.jsp">
  <c:forEach items="${options}">
    <xmp:buildCheckbox name="choice" 
      selections="${paramValues.choice}" />
    <br>
  </c:forEach>
  <input type="submit">
</form>

Here the JSTL <c:forEach> action loops through a Map with option texts and values, the same way as in the previous example. The <xmp:buildCheckbox> action generates a checkbox element using the specified name ...

Get JavaServer Pages, 3rd Edition 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.