JSTL break ? possible

How to beak a loop in JSTL

c:forEach is a JSTL loop. but suppose i want to break the loop at some point. how to do that ?
cofactorAsked:
Who is Participating?
 
Jim CakalicSenior Developer/ArchitectCommented:
cofactor,

I didn't mean to mislead you. What I meant was that perhaps you were looking for break when the problem could be solved another way. It sounds now like maybe this is just a hypothetical question.

As Tim correctly advised, there is no break facility in the c:ForEach tag. And his suggestion to use the jelly tags should be a suitable alternative. On a positive note, it looks like the break tag might be coming in JSTL 1.2. Inspired, as it seems, by jelly. Unfortunately, that's no help today.

The only two theoretical possibilities I can think of for breaking c:ForEach is a) fake it or b) throw it.

a) Faking a break involves setting a variable that can be tested and, when true, bypassing the body of the loop. It doesn't really break the loop, of course. You still end up iterating over the entire collection, however large it may be.

<c:set var="break" value="false"/>
<c:forEach var="item" items="${collection}">
  <c:if test="${!break}">
     ...
     <c:set var="found" value="true"/>
  </c:if>
</c:forEach>

b) You could throw an exception. You really don't care about the exception, it's just a means to break the loop so surround the c:ForEach with a c:catch and then just ignore it.

<c:catch var="exception">
  <c:forEach var="item" items="${collection}">
      ...
      <% throw new Exception(""); %>
  </c:forEach>
</c:catch>

Jim
0
 
TimYatesCommented:
I don't believe there is a "break" mechanism...

You could write your own I suppose:

http://www.geocities.com/herong_yang/jsp/tag_parent.html

has a simple example of his own loop and break tag

You would need to look deeper into the forEach code to see how breaking that would be possible...

Tim
0
 
cofactorAuthor Commented:
No, i dont want to use any custiom tag other than JSTL tags.

so if i need to break a loop  how JSTL would help me ?
is it not possible ? do i have to write dirty scriptlets ? no other tricks ?

0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
TimYatesCommented:
>> No, i dont want to use any custiom tag other than JSTL tags.

There is no "break" tag in JSTL...

There's the apache commons "jelly" tags...  That has a forEach tag and a break tag...

http://jakarta.apache.org/commons/jelly/tags.html
0
 
Jim CakalicSenior Developer/ArchitectCommented:
Hi,

Why do you want to break the forEach loop? Maybe there's a different way of going about this?
0
 
cofactorAuthor Commented:
>Why do you want to break the forEach loop? Maybe there's a different way of going about this?

plz plz give an example.  but sometimes we need break !  how JSTL can ignore that !

you told there is some other way to do that . what is that way ?

N.B : i do not know other Apache common tags except JSTL core tags .





i just wanted to know how you can tackle the  problem when u need a break ?


0
 
TimYatesCommented:
What's wrong with the Jelly tags I gave a link for?
0
 
cofactorAuthor Commented:
Tim
===

i visited the link you pointed out. but i could not understand it . because there was no example


Jim
===

i have understood your solution (b). Its  fine.


but could not understand  the solution  (a).

 is solution (a) correct ? you have two variable "found" and "break".
0
 
Jim CakalicSenior Developer/ArchitectCommented:
Yes. Too early for me. I meant for both sets to be on var "break". Sorry.
0
 
JoeCommented:
Is there a way to access the counter in the forEach?  If so, maybe you can set it = to the size of your collection.

Joe
0
 
cofactorAuthor Commented:
thank you, i got my full solution.
thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.