I have a forEach loop in a jsp that loops through a list of objects and displays html table data for each object. I want to have an export link under each table for different data formats and I am able to pass the list of objects to an export controller.
My question is: Since I am going to the controller each time though the jsp loop, I don't know how to retain the index so I can export the current object only. Do I
1.) Put a counter in the jsp loop and pass it to the controller to determine the current list index?
2.) Have some kind of counter in my implementation class that will increment and retain its value each time I come through from jsp?
or is there a better way?
I'm not sure what you mean by "going to the controller each time through the jsp loop". You should only be hitting the controller once, and that's when your controller should be putting your list of elements into the model.
For the index, you want to use the varStatus
attribute. This object has an index
property. See here.
For example:
<c:forEach var="element" items="${elements}" varStatus="status">
Index is ${status.index}<br />
</c:forEach>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With