问题
如何使用JSTL检查集合的大小?
就像是:
<c:if test="${companies.size() > 0}">
</c:if>
#1 热门回答(399 赞)
来源: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
而不是>
as>
。
#2 热门回答(27 赞)
正如@Joel和@Mark Chorley先前在评论中所建议的那样:
${empty companies}
这将检查null和空列表/集合/数组。它没有得到你的长度,但它满足OP中的例子。如果你能摆脱它,这比导入标签库及其硬壳语法如gt
更清晰。
#3 热门回答(10 赞)
你可以像这样使用
${fn:length(numList)}