Loop through nothing.
We all know that using jstl we can us a <c:forEach…> to loop through just about anything, for example in the new NT2.2 we used a loop through all SKUs to grab gallery images for all apparel products.
<c:forEach var="items" items="${product.publishedItems}"> |
This is great but required all publishedItems in order for the loop to be effective.
But what if you want to loop through nothing? I know I know, sounds like it could be pretty useless but it’s not. What I’m talking about is what if you want to run a loop 5 times but have nothing to loop through? Well it’s pretty simple but difficult to find any documentation on, hence this post. So before you go dropping scriplet (or as I like to call it, “Nava” or “NinJava”) code on your .jsp and have one of your teammates commit treason, read on Josephine, read on.
Brace yourself, here it is.
<c:forEach begin="1" end="8"> |
Pretty neat huh, your loop starts at one and ends at 8, one thing I’ve yet to find is an effective way to break the loop using jstl if necessary. Ninja said something about setting a flag and I did that but at second glance it’s not quite as effective as I had originally hoped.
Doo said,
I just used this and it was super useful one more thing, I needed to use the number that the loop was on. This is made accessible just by adding var=”number” to the forEach and you can use it just like you would any other variable. Great post!
Add A Comment