This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JSP and the fly likes Change the <c:forEach> end index at runtime in JSP. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Change the <c:forEach> end index at runtime in JSP." Watch "Change the <c:forEach> end index at runtime in JSP." New topic
Author

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

Samir Banerjee
Ranch Hand

Joined: Jun 21, 2010
Posts: 72
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

Joined: Jul 06, 2005
Posts: 1016
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

Joined: Jan 10, 2002
Posts: 60774
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Samir Banerjee
Ranch Hand

Joined: Jun 21, 2010
Posts: 72
Sorry for the late reply! Will surely try something else to fix the case :)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Change the <c:forEach> end index at runtime in JSP.
 
Similar Threads
c:set problem
JSTL - total count for loop within a loop
Best choice for pagination?
How do I set this variable in JSTL?
iterating over hashmap using jstl