EL:empty的用法,JSTL <c:if test GOOD

79 篇文章 0 订阅

使用到jsp设计页面时,EL表达式是少不了的,有时候需要判断一个对象或一个数组是否为空,可以使用empty。

例如:有一个list数组,判断它是否有值,可以使用<c:if test="${empty list}">数组为空</c:if> <c:if test="${!empty list}">数组不为空</c:if>

对于对象的判断也是如此。

 

百度了一下相关文档还说明

empty用来对一个空变量值进行判断: null、一个空String、空数组、空Map、没有条目的Collection集合

这种方式的判断就比jstl的<c:if test="${fn:length(list)==0}">数组为空</c:if> 这种好用多了,也好记。


JSTL 入门: 表达式语言https://www.ibm.com/developerworks/cn/java/j-jstl0211/

EL 擅长寻找对象及其特性,然后对它们执行简单操作;它不是编程语言,甚至不是脚本编制语言。但是,与 JSTL 标记一起使用时,它就能使用简单而又方便的符号来表示复杂的行为。EL 表达式的格式是这样的:用美元符号($)定界,内容包括在花括号({})中,如清单 3 所示。


----------------------------------------------------------------------------------------------------

http://stackoverflow.com/questions/2811626/evaluate-empty-or-null-jstl-c-tags


You can use the <c:if> or <c:choose> for this.

<c:if test="${empty var1}">
    var1 is empty or null.
</c:if>
<c:if test="${not empty var1}">
    var1 is NOT empty or null.
</c:if>

or

<c:choose>
    <c:when test="${empty var1}">
        var1 is empty or null.
    </c:when>
    <c:otherwise>
        var1 is NOT empty or null.
    </c:otherwise>
</c:choose>

The ${not empty var1} can also be done by ${!empty var1}.

To learn more about those ${} things (the Expression Language, which is a separate subject fromJSTL), check here.


  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值