• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 16619
  • Last Modified:

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 ?
0
cofactor
Asked:
cofactor
  • 4
  • 3
  • 3
  • +1
1 Solution
 
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
 
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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
 
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now