You are viewing a plain text version of this content. The canonical link for it is here.
Posted to taglibs-dev@jakarta.apache.org by Sean Pritchard <sp...@buildnet.com> on 2001/05/09 21:09:58 UTC

bug fix for ForTag.java

I downloaded the 5/9 nightly build of the utility tags and ran into a small
bug with the For Tag.  I was using the tag to display the elements of an
array and using the array's length as input for the iterations parameter.
During runtime, I happened to have a zero length array (the arrays
represented results of a database search).  I got an
ArrayIndexOutOfBoundsException.  

After examining the ForTag.java code, I found that the logic had been
implemented to always execute the tag body once and then check the iteration
parameter.  This seemed more like a do...while loop than a for loop to me.
I modified the doStartTag() method to check the iteration parameter before
executing the body.  This solved the problem.  Here is the new doStartTag()
method...

public int doStartTag() {
      if (count < getIterations()) {
    	  return EVAL_BODY_TAG;
      }
      return SKIP_BODY;
    }

And here is the jsp page I used for testing (it will demonstrate the
original error as well as the fact that it is now fixed).

<html>
<body bgcolor="white">

<%@taglib uri="/WEB-INF/utility.tld" prefix="jLib" %>

<%! String [] color = new String[0]; %>

Testing For with 0-length array...
<ul>
<jLib:for varName="j" iterations="<%= color.length %>" >
    Inside For...	
    <li>  <%= color[j.intValue()] %>
  
</jLib:for>
</ul>
Test Complete!

<p>Counting to 10...<br>
<jLib:for varName="i" iterations="10" >
    <%= i.intValue() %><br>  
</jLib:for>

</body>
</html>
  

____________________
Sean Pritchard
smpritchard@yahoo.com