Correct syntax to compare values in JSTL <c:if test="${values.type}=='object'"> [duplicate]
I have an if
statement that I am trying to perform with JSTL.
My code is below (the variables values is an ArrayList that contains a user defined object and type is a private property of that object, with public getter/setter methods):
<c:forEach items="${list}" var="values">
<c:if test="${values.type}=='object'">
<!-- code here -->
</c:if>
</c:forEeach>
What would be the correct syntax of the part within the test
attribute. The docs don't really help with that part http://download.oracle.com/javaee/5/jstl/1.1/docs/tlddocs/index.html
Thanks.
The comparison needs to be evaluated fully inside EL ${ ... }
, not outside.
<c:if test="${values.type eq 'object'}">
As to the docs, those ${}
things are not JSTL, but EL (Expression Language) which is a whole subject at its own. JSTL (as every other JSP taglib) is just utilizing it. You can find some more EL examples here.
<c:if test="#{bean.booleanValue}" />
<c:if test="#{bean.intValue gt 10}" />
<c:if test="#{bean.objectValue eq null}" />
<c:if test="#{bean.stringValue ne 'someValue'}" />
<c:if test="#{not empty bean.collectionValue}" />
<c:if test="#{not bean.booleanValue and bean.intValue ne 0}" />
<c:if test="#{bean.enumValue eq 'ONE' or bean.enumValue eq 'TWO'}" />
See also:
- Our EL wiki page
By the way, unrelated to the concrete problem, if I guess your intent right, you could also just call Object#getClass()
and then Class#getSimpleName()
instead of adding a custom getter.
<c:forEach items="${list}" var="value">
<c:if test="${value['class'].simpleName eq 'Object'}">
<!-- code here -->
</c:if>
</c:forEeach>
See also:
- instanceof check in EL expression language
精彩评论