242

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

Something like:

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

</c:if>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Sergio del Amo
  • 76,835
  • 68
  • 152
  • 179

4 Answers4

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

</c:if>

This syntax works only in EL 2.2 or newer (Servlet 3.0 / JSP 2.2 or newer). If you're facing a XML parsing error because you're using JSPX or Facelets instead of JSP, then use gt instead of >.

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

</c:if>

If you're actually facing an EL parsing error, then you're probably using a too old EL version. You'll need JSTL fn:length() function then. From the documentation:

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 JSP page to allow the fn namespace:

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

Or if you're using JSPX or Facelets:

<... xmlns:fn="http://java.sun.com/jsp/jstl/functions">

And use like this in your page:

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

So to test with length of a collection:

<c:if test="${fn:length(companies) gt 0}">

</c:if>

Alternatively, for this specific case you can also simply use the EL empty operator:

<c:if test="${not empty companies}">

</c:if>
Martlark
  • 14,208
  • 13
  • 83
  • 99
  • 47
    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 Apr 21 '10 at 19:27
  • I agree with Mark here. Why import more cruft into your page for one tag? Use Mark's solution, it's cleaner. – ResourceReaper Feb 25 '14 at 01:12
  • Agreed - empty is cleaner. @Joel should post as a full answer so it can be voted up and accepted. A lot of us here (me) are non-jsp programmers copy/pasting together snippets from Google and SO without much thought beyond the first piece of code we see. – Josh Jun 12 '14 at 10:30
  • 11
    The OP asked about checking the size, not necessarily about checking if it is empty, my answer also allows more complicated scenarios to be contemplated with the full tag library available. – Martlark Jun 14 '14 at 07:50
  • companies must be a var not a param, otherwise you have to define it, e.g. for ATG - – Andriy Boyko Jul 10 '14 at 07:32
  • 1
    Thanks, mate. It was really helpful as I wanted to display the list only when the size was more than one. – Adil Aslam Sachwani Aug 26 '20 at 13:09
30

As suggested by @Joel and @Mark Chorley in earlier comments:

${empty companies}

This checks for null and empty lists/collections/arrays. It doesn't get you the length but it satisfies the example in the OP. If you can get away with it this is just cleaner than importing a tag library and its crusty syntax like gt.

Josh
  • 4,894
  • 6
  • 34
  • 42
14

You can use like this

${fn:length(numList)}
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
-2

use ${fn:length(companies) > 0} to check the size. This returns a boolean

Supun Dharmarathne
  • 1,138
  • 1
  • 10
  • 19