Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retaining an index for Controller inside jsp foreach

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?

like image 439
jimdrang Avatar asked Dec 13 '22 09:12

jimdrang


1 Answers

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>
like image 188
Vivin Paliath Avatar answered Jan 08 '23 08:01

Vivin Paliath