Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Change the <c:forEach> end index at runtime in JSP.

 
Samir Banerjee
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
My requirement is something like this :



So basically I want Index to increase at run time while the for loop is executing. In the above case although the Index increases but does not reflect in the for loop.
How can I achieve this in JSP. Any kind of help will be much appreciated. Thanks in advance.
 
Stefan Evans
Bartender
Pie
Posts: 1669
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So if I understand this right, while looping through your items you can add more to your list?

Using this syntax the end index would be evaluated at the start of the loop.
It would not get re-evaluated every time through, so modifying the value $Index wouldn't change the amount of iterations.

I can't think of any way to do this with JSTL syntax directly.
One hacky workaround I can think of would be to implement an Iterator that has this functionality, and invoke the forEach tag referencing the iterator.




 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64613
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would back up and bit and ask why? Seems like a dicey thing to be doing and I question whether there's a better approach.
 
Samir Banerjee
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for the late reply! Will surely try something else to fix the case :)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic