如何使用JSTL检查集合的大小?
类似的东西:
<c:if test="${companies.size() > 0}">
</c:if>
答案 0 :(得分:414)
来自:http://java.sun.com/products/jsp/jstl/1.1/docs/tlddocs/fn/tld-summary.html
length(java.lang.Object) - 返回集合中的项目数或字符串中的字符数。
将它放在页面顶部以允许fn命名空间:
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
并在jsp页面中使用这样的内容:
<p>The length of the companies collection is : ${fn:length(companies)}</p>
所以要测试一个集合的长度:
<c:if test="${fn:length(companies) gt 0}">
<p>It is greater than 0</p>
</c:if>
请注意,jsp / jsf标记内不允许使用gt
代替>
>
。
答案 1 :(得分:30)
正如@Joel和@Mark Chorley先前在评论中所建议的那样:
${empty companies}
这将检查null和空列表/集合/数组。它没有得到你的长度,但它满足OP中的例子。如果你可以侥幸成功,那么这比导入标签库及其gt
之类的硬壳语法更清晰。
答案 2 :(得分:11)
你可以这样使用
${fn:length(numList)}
答案 3 :(得分:-2)
使用${fn:length(companies) > 0}
检查尺寸。这将返回一个布尔值