permaculture playing cards
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
JavaRanch » Java Forums » Java » JSP
Bookmark " Watch " New topic

'break' functionality in a forEach tag ?

John Stein

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,

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

Joined: Jan 10, 2002
Posts: 63852

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] [About Bear] [Books by Bear]
John Stein

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.

I agree. Here's the link:
subject: 'break' functionality in a forEach tag ?
It's not a secret anymore!