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