PrinzLangweilig
PrinzLangweilig

Reputation: 3

If case in JSP with checking variable

I was trying to check if the counter runs two times. I need it therefore, that the movie reviews aren't shown twice.

But, I get an empty result for the reviews/persons.

<c:set var="counter" value="${0}"/>

<c:forEach items="${ reviews }" var="review">
    <c:forEach items="${ persons }" var="person">
        <c:if test="{ counter % 2 == 0}">
            <div class="review">
                <p>
                    <div class="user">
                        Benutzer: <c:out value="${ person.email }"></c:out>
                    </div>

                    <div class="stars">
                        Einzelbewertung: <c:out value="${ review.stars }"></c:out>
                    </div>

                    <div style="color: grey; font-size: 14px;">Meinung:</div>
                    <div class="reviewText">
                        <c:out value="${ review.text }"></c:out>
                    </div>
                </p>
            </div>
            <c:set var="counter" value="${counter + 1}" />

        </c:if>

    </c:forEach>

Upvotes: 0

Views: 1036

Answers (1)

Matt Ball
Matt Ball

Reputation: 359876

There is no need to use a separate counter. Use the forEach tag's varStatus.index or varStatus.count instead. See http://www.ibm.com/developerworks/java/library/j-jstl0318/ for more.

Something like this (note that status.index starts at zero for each review):

<c:forEach items="${ reviews }" var="review">
    <c:forEach items="${ persons }" var="person" varStatus="status">
        <c:if test="${ status.index % 2 == 0}">
            <div class="review">
                <p>
                    <div class="user">
                        Benutzer: <c:out value="${ person.email }"></c:out>
                    </div>

                    <div class="stars">
                        Einzelbewertung: <c:out value="${ review.stars }"></c:out>
                    </div>

                    <div style="color: grey; font-size: 14px;">Meinung:</div>
                    <div class="reviewText">
                        <c:out value="${ review.text }"></c:out>
                    </div>
                </p>
            </div>

        </c:if>

    </c:forEach>
</c:forEach>

Upvotes: 1

Related Questions