jakarta-taglibs-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Sean Pritchard <spritch...@buildnet.com>
Subject bug fix for ForTag.java
Date Wed, 09 May 2001 19:09:58 GMT
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


Mime
View raw message