File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes 'break' functionality in a forEach  tag ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » JSP
Bookmark " Watch " New topic
Author

'break' functionality in a forEach tag ?

John Stein
Greenhorn

Joined: Jan 26, 2006
Posts: 4
Hi there,

Does anyone know of a way to 'break' out of a JSTL forEach tag - without using scripting ? I'm using the 'items' format of the tag as opposed to the 'begin / end' format and would like to break out of the loop (based on the value of an EL variable) to save having to loop needlessly around a, potentially large, structure.

I've had a dig around and can't find any official solution to this problem. Can anyone think of a decent workaround ?

Thanks in advance,

John.
[ February 20, 2006: Message edited by: John Stein ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60041
    
  65

No, there is no break mechanism.

You could put an if condition inside the loop that skips over the body once you detect your "break" value, but the best work-around would be to re-structure the collection such that no breaking is necessary in the first place.

When creating scriptless pages in JSP, it's important to make a slight shift in attitude and approach: make the data cater to the page, rather than the other way around.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
John Stein
Greenhorn

Joined: Jan 26, 2006
Posts: 4
Yes, the-condition-inside-the-loop that you mentioned was really the only workaround I could come up with. Only thing is that it still enters the loop for the maximum number of times. But, I think its probably the approach I'll need to take.

In the design stage, I did originally consider 'tailoring' the data to suit the page, but this approach would have meant the controller handling stuff that is completely view-centric. I think you're right tho, the scriptless JSP approach does require a slight shift in attitude and a slight move away from the perceived wisdom.

Thanks for your response.

John.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: 'break' functionality in a forEach tag ?
 
Similar Threads
Want to use a jstl value in another expression
How to break c:forEach iteration?
Can we put EL in a for loop?
How to Print Serial number on jsp?
tag to loop a List