Bic Mitchun
Bic Mitchun

Reputation: 518

java.lang.IllegalArgumentException when trying to execute JSTL < c:if > condition

I'm trying to tell my program to print out a message on every product if there is less than 5 products in a given category. But when I test the condition in my browser, when I click on a category (even if the category has more than 5 prods.), the page goes blank and the logs flag this following error:

java.lang.IllegalArgumentException: Cannot convert {[entity.Product[ id=36 ], entity.Product[ id=37 ], entity.Product[ id=38 ], entity.Product[ id=39 ], entity.Product[ id=160 ]]} of type class org.eclipse.persistence.indirection.IndirectList to class java.lang.Long

I think there is something wrong with my c:if condition.

    <c:set var="product" value="${categoryProducts}"/>

<c:if test="${categoryProducts > 5}">
                         <div id="pd_msg">SALE!</div>
                          </c:if>

Upvotes: 0

Views: 285

Answers (2)

Bic Mitchun
Bic Mitchun

Reputation: 518

The solution suggested by @BadK is good, but in case it does not work for you like it did not for me, the code below worked for me:

 <c:if test="${fn:length(categoryProducts) < 5}">
        <div id="pd_msg">SALE!</div>
    </c:if>

This solution was found here: Evaluate if list is empty JSTL

Upvotes: 0

BadK
BadK

Reputation: 307

Try this:

    <c:set var="product" value="${categoryProducts}"/>

    <c:if test="${categoryProducts.size() > 5}">
        <div id="pd_msg">SALE!</div>
    </c:if>

The Exception told you that you are trying to compare a List(left side) against a Long(right side). But you can only compare objects of the same type and thats why you need to call size() first, which will give you the total amount of items in the list.

Upvotes: 3

Related Questions