正确的语法来比较JSTL中的值< c:if test =" $ {values.type} =='object'"> [英] Correct syntax to compare values in JSTL <c:if test="${values.type}=='object'">

查看:509
本文介绍了正确的语法来比较JSTL中的值< c:if test =" $ {values.type} =='object'">的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 if 语句,我试图用JSTL执行。

I have an if statement that I am trying to perform with JSTL.

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

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>

测试中部件的正确语法属性。文档对该部分没有实际帮助 http: //download.oracle.com/javaee/5/jstl/1.1/docs/tlddocs/index.html

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

谢谢。

推荐答案

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

The comparison needs to be evaluated fully inside EL ${ ... }, not outside.

<c:if test="${values.type eq 'object'}">

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

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'}" />



参见:




  • 我们的EL维基页面

  • See also:

    • Our EL wiki page
    • 顺便说一句,与具体问题无关,如果我猜你的意图正确,你也可以调用 Object#getClass() 然后 Class#getSimpleName() 而不是添加自定义getter。

      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>
      



      参见:




      • 检查EL表达语言的实例

      • See also:

        • instanceof check in EL expression language
        • 这篇关于正确的语法来比较JSTL中的值&lt; c:if test =&quot; $ {values.type} =='object'&quot;&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆