How to compare 2 variables with <c:if>

15,931

All JSTL tags (as well as XML and HTML tags) require an opening tag and a closing tag. The opening tag defines where the tag body starts (and also allows you to define attributes). The closing tag defines where the tag body ends.

In the following code sample, the closing tag is on the last line.

<c:if test="${category.id == test}" >
   <b>Test passed!</b>
</c:if>

As you can see, it has the same name as the opening tag, and starts with a / (and has no attributes).

The first two opening tags that you included in your question should work.

Share:
15,931
Sergey
Author by

Sergey

Updated on June 25, 2022

Comments

  • Sergey
    Sergey almost 2 years

    I am using jsp + spring mvc, and on jsp page I have some list - catList with idand name and also have some variable test. I am trying to compare cat.id and test, but can't because every time have runtime syntax errors:

    <c:forEach var="cat" items="${catList}" varStatus="i">
      <c:out value="${cat.id}"/>
     <%-- comparison and some action--%>
    </c:forEach>
    

    tried:

    <c:if test="${category.id == test}" >
    <c:if test="${category.id eq test}" >
    <c:if test="${category.id eq ${test}}">
    

    Update: I resolved this problem, simply it was error with server redeploy

    • Montre
      Montre over 12 years
      What syntax errors? Because the first or second variant look okay to me.
    • Dave Newton
      Dave Newton over 12 years
      When you get an error it's helpful to post the error--otherwise we're just guessing. The first two tests are perfectly valid. What types are the id and test?
    • Sergey
      Sergey over 12 years
      id - is long and test is Long, unfortunately I can't reproduce error now, but error log show - all <c:if/> fragment and say that it's not valid