How to compare 2 variables with <c:if>
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.
Sergey
Updated on June 25, 2022Comments
-
Sergey almost 2 years
I am using jsp + spring mvc, and on jsp page I have some list -
catList
withid
andname
and also have some variabletest
. I am trying to comparecat.id
andtest
, 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 over 12 yearsWhat syntax errors? Because the first or second variant look okay to me.
-
Dave Newton over 12 yearsWhen 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
andtest
? -
Sergey over 12 yearsid - 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
-