tags:

views:

3320

answers:

1

How can i check the size of a collection with JSTL?

Something like:

<c:if test="${companies.size() > 0">

</c:if>
+14  A: 

from: http://java.sun.com/products/jsp/jstl/1.1/docs/tlddocs/fn/tld-summary.html

length( java.lang.Object) - Returns the number of items in a collection, or the number of characters in a string.

put this at the top of the page to allow the fn namespace

 <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

and use like this in your jsp page

<p>The length of the companies collection is : ${fn:length(companies)}</p>

(from memory)

So to test with length of a collection:

<c:if test="${fn:length(companies) gt 0}">
   <p>It is greater than 0</p>
</c:if>

Note the use of gt instead of > as > is not allowed inside jsp/jsf tags.

Martlark
Just wanted to note that if the need to get the size is to determine if the collection is non empty then the "empty" operator is useful since it also checks for null.
Joel