简体   繁体   中英

jstl <c:when> tag wrong result

<c:set var="pageCount" value="16"></c:set>
<c:set var="maxSugguestPage" value="7"></c:set>
<c:choose>
    <c:when test="${pageCount < maxSugguestPage}"> <!-- why this block executed? -->
        <c:forEach begin="1" end="${pageCount}" var="index">
            <li><a href="#">${index }</a></li>
        </c:forEach>
    </c:when>
    <c:otherwise>
        <c:set var="firstDot" value="true"></c:set>
        <c:forEach begin="1" end="${pageCount - 1}" var="index">
        <c:choose>
            <c:when test="${index > maxSugguestPage and firstDot == true}">
                <li><a href="#">..</a></li>
                <c:set var="firstDot" value="false"></c:set>
            </c:when>
            <c:otherwise>
                <c:if test="${firstDot == true}">
                    <li><a href="#">${index }</a></li>
                </c:if>
            </c:otherwise>
        </c:choose>
        </c:forEach>
        <li><a href="#">${pageCount }</a></li>
    </c:otherwise>
</c:choose>

this is a jsp file using jstl ,but there are some problem with the tag <c:when> .
I declare two variables called pageCount and maxSuggestPage , it's very obvious pageCount > maxSugguestPage , but when using <c:when test="${pageCount < maxSuggestPage}"> , the code block in it executed and the <c:otherwise> block not.

You have set the values as strings, not as numbers. Those strings are lexicographically compared.

Set them as numbers instead.

<c:set var="pageCount" value="${16}" />
<c:set var="maxSugguestPage" value="${7}" />

Unrelated to the concrete problem, it's unnecessary to compare a boolean expression to a fixed boolean value in a boolean statement like so ${firstDot == true} . Just use the boolean expression right away like so ${firstDot} .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM