Break A forEach Loop Using JSTL

To break a c:forEach loop in JSTL, well, is impossible. You would have to add in some extra variables and maybe a varStatus variable to do some conditional statements and break out of the loop if those condition(s) is/are met. If a JSTL attribute did exist to break out of a c:forEach loop, then it would ease programmer’s work of adding extra code to your existing one. But then again, there is none.

Related Posts Plugin for WordPress, Blogger...

3 comments

  1. A break tag can be implemented, e.g. in the Apache JSTL EL implementation. Just write a new tag (say ForEachBreakTag.java):

    then extend the ForEachTag and override the hasNext method, so that when the loop’s collection is released it doesn’t throw a null pointer:

    If you change the TLD to use these classes then you can break your loop:

  2. Hi Stu,

    Great response. What changes should I do to TLD to specify that I am overriding ForEachTag? I am using spring.

Leave a Reply

Your email address will not be published. Required fields are marked *