正确的语法来比较JSTL中的值<c:if test ="$ {values.type} =='object'">

Ank*_*kur 17 jsp if-statement jstl el

我有一个if声明,我正在尝试使用JSTL.

我的代码如下(变量值是包含用户定义对象的ArrayList,类型是该对象的私有属性,使用公共getter/setter方法):

<c:forEach items="${list}" var="values">
    <c:if test="${values.type}=='object'">
        <!-- code here -->
    </c:if>
</c:forEeach>
Run Code Online (Sandbox Code Playgroud)

test属性中部件的正确语法是什么.文档并没有真正帮助那部分http://download.oracle.com/javaee/5/jstl/1.1/docs/tlddocs/index.html

谢谢.

Bal*_*usC 43

需要在EL内部完全评估比较${ ... },而不是在外部.

<c:if test="${values.type eq 'object'}">
Run Code Online (Sandbox Code Playgroud)

至于文档,那些${}东西不是JSTL,而是EL(表达语言),它本身就是一个完整的主题.JSTL(与其他所有JSP taglib一样)只是使用它.您可以在这里找到更多EL示例.

<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'}" />
Run Code Online (Sandbox Code Playgroud)

也可以看看:


顺便说一句,与具体问题无关,如果我猜你的意图正确,你也可以只是打电话Object#getClass()然后Class#getSimpleName()而不是添加一个自定义的getter.

<c:forEach items="${list}" var="value">
    <c:if test="${value['class'].simpleName eq 'Object'}">
        <!-- code here -->
    </c:if>
</c:forEeach>
Run Code Online (Sandbox Code Playgroud)

也可以看看: