How to compare parameters in JSTL

12,507

Solution 1

<c:when test="${list.id} == 1">

This is wrong. You need to put the entire expression inside ${}.

If getId() returns a Number (integer, long, etc), then use the following:

<c:when test="${list.id == 1}">

Or if it returns a String (which is unnatural by the way), then use the following:

<c:when test="${list.id == '1'}">

Or if it returns a boolean (just as an example), then use the following:

<c:when test="${list.id}">

Solution 2

it should be <c:when test="${list.id == '1'}">
if you use <c:when test='${list.id == "1"}'> exception will be thrown

Share:
12,507
Moham Qmaizer
Author by

Moham Qmaizer

Updated on June 04, 2022

Comments

  • Moham Qmaizer
    Moham Qmaizer almost 2 years

    I have list of ID's from a database and pass them in a request to a servlet, but i can't compare them to any number.

    I think maybe i have to convert them to integer type:

        <c:forEach items="${subjec.id}" var="x">
    ${x}
      </c:forEach>
    <c:forEach items="${listPage}" var = "list">
    ${list.id} 
    <c:choose>
    <c:when test="${list.id} == 1">
    
    </c:when>
    </c:choose>
    
    </c:forEach>
    <c:if test="${subject1.id == 1}"> 
    ${subject1.id}
    </c:if>
    

    However, I cant test based on Integer format. Any suggetions for this case?