12.4. Accessing the Loop Status

The forEach tag defines an optional status attribute that specifies the name of an attribute referring to an IteratorTagStatus object. This object provides details about the loop status. In particular, the IteratorTagStatus object has the following bean properties.

  • current. The current object, the same as you could obtain from the iteration variable that the var attribute specifies. This property corresponds to the getCurrent method and is of type Object.

  • index. The actual zero-based index of the current item within its data structure. This property corresponds to the getIndex method and is of type int.

  • count. The iteration number. This value starts at 1 and increases by 1 each time around the loop, regardless of the values of the begin, end, or step attributes. This property corresponds to the getCount method and is of type int.

  • first. A flag indicating whether the current item is the first in the iteration. Since this is a boolean property, it corresponds to the isFirst (not getFirst !) method.

  • last. A flag indicating whether the current item is the last in the iteration. Since this is a boolean property, it corresponds to the isLast (not getLast) method.

  • beginSpecified. A flag indicating whether the begin attribute was specified explicitly. Since this is a boolean property, it corresponds to the isBeginSpecified (not getBeginSpecified) method.

  • begin. The value of the begin attribute or –1 if begin is not specified. This property corresponds to the getBegin method and is of type int.

  • endSpecified. A flag indicating whether the end attribute was specified explicitly. Since this is a boolean property, it corresponds to the isEndSpecified (not getEndSpecified) method.

  • end. The value of the end attribute or –1 if end is not specified. This property corresponds to the getBegin method and is of type int.

  • stepSpecified. A flag indicating whether the step attribute was specified explicitly. Since this is a boolean property, it corresponds to the isStepSpecified (not getStepSpecified) method.

  • step. The value of the step attribute or –1 if step is not specified. This property corresponds to the getStep method and is of type int.

Listing 12.23 shows the jr version of a page that loops down a comma-delimited String and displays status information each time around the loop. Figure 12-14 shows the result. Listing 12.24 (Figure 12-15) shows the jx equivalent.

Figure 12-14. Result of status-loop-jr.jsp.


Figure 12-15. Result of status-loop-jx.jsp.


Listing 12.23. status-loop-jr.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<HTML> 
<HEAD> 
<TITLE>Loop Status: "jr" Version</TITLE> 
<LINK REL=STYLESHEET 
      HREF="../styles.css" 
      TYPE="text/css"> 
</HEAD> 

<BODY> 
<TABLE BORDER=5 ALIGN="CENTER"> 
  <TR><TH CLASS="TITLE"> 
      Loop Status: "jr" Version 
</TABLE> 
<P> 
<TABLE BORDER=1> 
<TR><TH CLASS="HEADING">Current<BR>Item 
    <TH CLASS="HEADING">Index 
    <TH CLASS="HEADING">Count 
    <TH CLASS="HEADING">First? 
    <TH CLASS="HEADING">Last? 
    <TH CLASS="HEADING"><CODE>begin</CODE><BR>Specified? 
    <TH CLASS="HEADING"><CODE>begin</CODE> 
    <TH CLASS="HEADING"><CODE>end</CODE><BR>Specified? 
    <TH CLASS="HEADING"><CODE>end</CODE> 
    <TH CLASS="HEADING"><CODE>step</CODE><BR>Specified? 
    <TH CLASS="HEADING"><CODE>step</CODE> 
<%@ taglib uri="http://java.sun.com/jsptl/ea/jr" prefix="jr" %>
						<jr:forEach status="status" begin="1" step="2"
						items='"a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z"'>
						<%@ page import="javax.servlet.jsptl.*" %>
						<% IteratorTagStatus status =
						(IteratorTagStatus)pageContext.getAttribute("status"); %> 
  <TR><TD><%= status.getCurrent() %> 
      <TD><%= status.getIndex() %> 
      <TD><%= status.getCount() %> 
      <TD><%= status.isFirst() %> 
      <TD><%= status.isLast() %> 
      <TD><%= status.isBeginSpecified() %> 
      <TD><%= status.getBegin() %> 
      <TD><%= status.isEndSpecified() %> 
      <TD><%= status.getEnd() %> 
      <TD><%= status.isStepSpecified() %> 
      <TD><%= status.getStep() %>
						</jr:forEach> 
</TABLE> 

</BODY> 
</HTML> 

Listing 12.24. status-loop-jx.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<HTML> 
<HEAD> 
<TITLE>Loop Status: "jx" Version</TITLE> 
<LINK REL=STYLESHEET 
      HREF="../styles.css" 
      TYPE="text/css"> 
</HEAD> 

<BODY> 
<TABLE BORDER=5 ALIGN="CENTER"> 
  <TR><TH CLASS="TITLE"> 
      Loop Status: "jx" Version 
</TABLE> 
<P> 
<TABLE BORDER=1> 
<TR><TH CLASS="HEADING">Current<BR>Item 
    <TH CLASS="HEADING">Index 
    <TH CLASS="HEADING">Count 
    <TH CLASS="HEADING">First? 
    <TH CLASS="HEADING">Last? 
    <TH CLASS="HEADING"><CODE>begin</CODE><BR>Specified? 
    <TH CLASS="HEADING"><CODE>begin</CODE> 
    <TH CLASS="HEADING"><CODE>end</CODE><BR>Specified? 
    <TH CLASS="HEADING"><CODE>end</CODE> 
    <TH CLASS="HEADING"><CODE>step</CODE><BR>Specified? 
    <TH CLASS="HEADING"><CODE>step</CODE> 
<%@ taglib uri="http://java.sun.com/jsptl/ea/jx" prefix="jx" %>
						<jx:forEach status="status" begin="1" step="2"
						items="a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z"> 
  <TR><TD><jx:expr value="$status.current"/> 
      <TD><jx:expr value="$status.index"/> 
      <TD><jx:expr value="$status.count"/> 
      <TD><jx:expr value="$status.first"/> 
      <TD><jx:expr value="$status.last"/> 
      <TD><jx:expr value="$status.beginSpecified"/> 
      <TD><jx:expr value="$status.begin"/> 
      <TD><jx:expr value="$status.endSpecified"/> 
      <TD><jx:expr value="$status.end"/> 
      <TD><jx:expr value="$status.stepSpecified"/> 
      <TD><jx:expr value="$status.step"/>
						</jx:forEach> 
</TABLE> 

</BODY> 
</HTML> 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.38.253