正确的语法来比较JSTL中的值< c:if test =" $ {values.type} =='object'"> [英] Correct syntax to compare values in 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示例。/ p>
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维基页面
- Our EL wiki page
See also:
顺便说一句,与具体问题无关,如果我猜你的意图正确,你也可以调用 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表达语言的实例
- instanceof check in EL expression language
See also:
这篇关于正确的语法来比较JSTL中的值< c:if test =" $ {values.type} =='object'">的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!