ChatGPT解决这个技术问题 Extra ChatGPT

Increment counter with loop

This question is related to my previous question :

Jsp iterate trough object list

I want to insert counter that starts from 0 in my for loop, I've tried several combinations so far :

1.

<c:forEach var="tableEntity" items='${requestScope.tables}'>
   <c:forEach var="rowEntity" items='${tableEntity.rows}' varStatus="count">            
        <c:out value="${count}" />
    </c:forEach>
</c:forEach>

2.

<c:set var="count" value="0" scope="page" />
<c:forEach var="tableEntity" items='${requestScope.tables}'>
   <c:forEach var="rowEntity" items='${tableEntity.rows}'>      
   <%=count++%>  
<c:out value="${count}" />
    </c:forEach>
</c:forEach>

Problem with first approach is that outer loop has 3 items and inner loop has 7 items, so for each outer item the count starts from 0. The second one I get compile error. Here is basically what I want :

counter = 0;
outer for loop 
    inner for loop 
       counter++;
       //cout/echo/print counter value should start from 0
    end inner loop
end outer loop

I'm just not totally familiar with the syntax. thank you

What compiler error do you get?
Do not do it. It's hard to write and ever harder to read. Just create a bean and use it. In case you really need it: What about one counters per loop and combining the like outer*tableEntity.rows.size + inner?
@maaartinus can you give an example ?
I can't find it now, but it's simple: Whenever the JSP gets too complicated, create a bean (a class with default constructor and all fields settable), implement the required functionality in Java instead of this chevronitis, set the fields for the computation and get the result. Here, the JSP solution is simpler than I thought, but in general don't put complex computations in JSP.

d
dogbane

Try the following:

<c:set var="count" value="0" scope="page" />

//in your loops
<c:set var="count" value="${count + 1}" scope="page"/>

this is exactly what I've looked for tnx
See the answer by BalusC for a much cleaner solution. ie, use varStatus
I dunno.. this one actually looks much cleaner and easier on the eyes
That's great thanks! My implementation required it to be a session scope for it to work though, cheers!
how can I assign count to a JavaScript variable?
B
BalusC

The varStatus references to LoopTagStatus which has a getIndex() method.

So:

<c:forEach var="tableEntity" items='${requestScope.tables}' varStatus="outer">
   <c:forEach var="rowEntity" items='${tableEntity.rows}' varStatus="inner">            
        <c:out value="${(outer.index * fn:length(tableEntity.rows)) + inner.index}" />
    </c:forEach>
</c:forEach>

See also:

Hidden features of JSP/Servlet


@BalusC your code seems interesting but I don't really get what does it do, but it doesn't produce the correct result. Thank you for your time and effort.
@BalusC yes, I used this one. Outer loop has 3 items, first inner loop has 7 items, second inner loop has 10 items, and third inner loop has 3 items. So in total counter should count from 0 until 19. But your code produces the following result : 0 1 2 3 4 5 6 10 11 12 13 14 15 16 17 18 19 6 7 8 . Maybe I didn't explain my question properly .. but this isn't what I wanted to achieve. And I don't think ur error was stupid because all of this looks the same to me(for the time being), but that will change.
In your original question you had 2 loops. The answer was targeted on that. If you have more nested loops, then you have to take them all into account in the calculation and then the answer of dogbane is indeed less complex (however the scope="page" is superfluous if you don't have a count attribute in other scopes, you could then safely remove it).
@BalusC yes indeed I didn't know that, I don't need scope page. thank you, can you recommend some useful resources for JSP, I have a programming background but this is my entry into springmvc/jsp.
Start at our JSP info page which you can also access by hovering the [jsp] tag and clicking info link or by clicking about the jsp tag link when facing all questions with the same tag after clicking [jsp] tag.
A
Andy Brudtkuhl

You can use varStatus in your c:forEach loop

In your first example you can get the counter to work properly as follows...

<c:forEach var="tableEntity" items='${requestScope.tables}'>
   <c:forEach var="rowEntity" items='${tableEntity.rows}' varStatus="count">            
        my count is ${count.count}
    </c:forEach>
</c:forEach>

B
Basheer AL-MOMANI

what led me to this page is that I set within a page then the inside of an included page I did the increment

and here is the problem

so to solve such a problem, simply use scope="request" when you declare the variable or the increment

//when you set the variale add scope="request"
<c:set var="nFilters" value="${0}" scope="request"/>
//the increment, it can be happened inside an included page
<c:set var="nFilters" value="${nFilters + 1}"  scope="request" />

hope this saves your time