8

I want to do with the jstl something like this:

int i=0;
int j=0;

<c:forEach items="${commentNames}" var="comment">     

     <c:forEach items="${rates}" var="rate">

        <c:if test="${i==j}">

          int i++

        </c:if> 

     </c:forEach> 

  int j++;

</c:forEach> 

Is this possible with the jstl?When i try this it hit me errors and i was wondering if there is a correct way way to write it

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Nick Robertson
  • 1,047
  • 4
  • 18
  • 41
  • 1
    You'd want to use c:set to increment i, of you're trying to use i as an actual variable. – Dave Newton Jun 02 '12 at 23:09
  • 2
    While it *is* possible to do this with JSTL, I wouldn't recommend doing it there. Your JSP should be purely for presentation logic, not business calculations. – adarshr Jun 02 '12 at 23:10
  • I'm trying to use i as an actual variable – Nick Robertson Jun 02 '12 at 23:11
  • @adarshr i understand what you are telling me and i agree with you. But in this specific occasion that i want to use it there is no other way.So please if you know help me. – Nick Robertson Jun 02 '12 at 23:13
  • This is a case where XY breaks down. Although the OP should have asked about X, there are some legitimate reasons for doing Y _for presentation purposes_ and unfortunately we don't get Y except for possibly the comment of @DaveNewton. – demongolem Jul 11 '14 at 12:17

1 Answers1

9

Not directly, but you can use the varStatus to put an instance of LoopTagStatus in the scope of <c:forEach>. It offers several getters to figure among others the loop index and whether it's the first or the last iteration of the loop.

I'm only unsure how your <c:if> makes sense, but I think that you actually have two lists of the same size with comment names and comment rates and that you need to show only the rate at the same index as the comment.

<c:forEach items="${commentNames}" var="comment" varStatus="commentLoop">     
    ${comment}
    <c:forEach items="${rates}" var="rate" varStatus="rateLoop">
        <c:if test="${commentLoop.index == rateLoop.index}">
            ${rate}
        </c:if>
    </c:forEach> 
</c:forEach> 

This is however clumsy. You can better get the rate by index directly.

<c:forEach items="${commentNames}" var="comment" varStatus="commentLoop">     
    ${comment}
    ${rates[commentLoop.index]}
</c:forEach> 

Much better is to create a Comment object with a name and rate property.

public class Comment {

    private String name;
    private Integer rate;

    // Add/autogenerate getters/setters.
}

So that you can use it as follows:

<c:forEach items="${comments}" var="comment">
    ${comment.name}
    ${comment.rate}
</c:forEach> 

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Man you are amazing. Thank you a lot for your help. You said " I think that you actually have two lists of the same size with comment names and comment rates and that you need to show only the rate at the same index as the comment". You are in my mind! – Nick Robertson Jun 02 '12 at 23:25
  • You're welcome. I'd however appreciate if you elaborate the concrete functional requirement better in your future questions :) – BalusC Jun 02 '12 at 23:26
  • I will do my best.Thank you again. – Nick Robertson Jun 02 '12 at 23:28
  • @BalusC Great !!! You know if i see an elaborate and very good answer, i always think it would be you. And always it was you. Great !!! – Ramesh PVK Jun 03 '12 at 03:45