You are viewing a plain text version of this content. The canonical link for it is here.
Posted to taglibs-user@tomcat.apache.org by sa...@automatedlogic.com on 2001/10/23 20:22:50 UTC

jsptl forEach and break

For the jsptl iterator tags (such as forEach), how can you break out of a 
loop early?  I would like the functionality you get in Java with "break" 
or "continue" statements, but don't see how to do this without scripting 
code.  Are new tags required for this?

Re: jsptl forEach and break

Posted by Shawn Bayern <ba...@essentially.net>.
On Tue, 23 Oct 2001 sappling@automatedlogic.com wrote:

> For the jsptl iterator tags (such as forEach), how can you break out
> of a loop early?  I would like the functionality you get in Java with
> "break"  or "continue" statements, but don't see how to do this
> without scripting code.  Are new tags required for this?

New tags would be required for this, but they are not implementable in JSP
1.2 and have thus are not planned for JSPTL 1.0.

The problem is how a <break> tag would signal the completion of a loop and
cause the loop to terminate immediately.  <break> could be implemented
merely as a 'signal' to the containing <forEach> tag to halt iteration
after the current round of iteration, but this would not be a true break
and would be confusing because of its differences with Java's 'break'
keyword.

<break> could be implemented better if it threw an exception meant to be
caught using JSP 1.2's TryCatchFinally interface, but the implementation
would still not be fully operable when custom tags were involved.  In a
situation like

   <forEach>
     <custom tag>
        <break/>
     </custom tag>
   </forEach>

the handler for <custom tag> would, if it implemented TryCatchFinally,
need to know to re-throw the exception that the inner <break> tag throws.  
If it didn't, then <break>'s message would be lost and it would have no
effect.  A <break> tag that we couldn't guarantee to work wouldn't be much
of a <break> tag!

<Continue> is even more problematic, since the TryCatchFinally
implementation would be completely untenable:  once an exception is caught
by doCatch(), there is no mechanism to "restart" the tag.

Thus, <break> and <continue> are not planned as JSPTL tags, at least not
in JSPTL 1.0.  You can emulate their behavior with proper structuring of
<if> tags.  While more verbose, this latter usage is more closely in line
with what's seen as a more typical JSP pattern:  JSP, being used mostly
for presentation, needs less intricate control flow than the Java language
itself provides.

Shawn Bayern
JSPTL reference-implementation lead