使用到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.


Logo

一站式虚拟内容创作平台,激发创意,赋能创作,进入R空间,遇见同道,让优质作品闪耀发光。​

更多推荐