Multiple Approaches to Check Collection Size with JSTL and Their Applicable Scenarios

Nov 21, 2025 · Programming · 9 views · 7.8

Keywords: JSTL | Collection Size Check | EL Expression

Abstract: This article comprehensively explores three main methods for checking collection size in JSP pages using JSTL: directly invoking the collection's size() method, utilizing the fn:length() function, and leveraging the empty operator. It analyzes the syntax characteristics, version requirements, and usage scenarios of each method, demonstrating practical applications through complete code examples. Compatibility solutions for different EL and JSTL versions are provided to help developers choose the most suitable implementation based on project needs.

Introduction

In Java web development, JSTL (JSP Standard Tag Library) is a crucial tool for handling page logic. Checking collection size is a common business requirement. Based on best practices, this article systematically introduces three primary implementation methods.

Method 1: Directly Invoking the Collection's size() Method

In environments supporting EL 2.2 or newer, you can directly invoke the collection's size() method:

<c:if test="${companies.size() > 0}">
    <!-- Logic when collection is not empty -->
</c:if>

This method requires Servlet 3.0 / JSP 2.2 or newer. In JSPX or Facelets, to avoid XML parsing errors, use gt instead of >:

<c:if test="${companies.size() gt 0}">
    <!-- Logic when collection is not empty -->
</c:if>

Method 2: Using the fn:length() Function

For older EL versions, JSTL provides the fn:length() function, applicable to collections, arrays, and strings:

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<p>The length of the companies collection is: ${fn:length(companies)}</p>
<c:if test="${fn:length(companies) gt 0}">
    <!-- Logic when collection is not empty -->
</c:if>

In JSPX or Facelets, introduce via XML namespace:

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

The fn:length() function returns the number of elements in the collection, and as shown in the reference article, it also applies to strings and arrays:

<c:set var="inputString" value="Example String"/>
<c:set var="stringList" value="${fn:split('First Second Third', ' ')}"/>
<p>String length: ${fn:length(inputString)}</p>
<p>Collection length: ${fn:length(stringList)}</p>

The output shows the string length and the number of collection elements, verifying the function's versatility.

Method 3: Using the empty Operator

The EL built-in empty operator offers a concise checking method:

<c:if test="${not empty companies}">
    <!-- Logic when collection is not empty -->
</c:if>

This method has concise syntax and good compatibility, suitable for most scenarios.

Version Compatibility and Selection Recommendations

Choose the appropriate method based on the project environment:

In practice, it is recommended to uniformly import the required tag libraries at the top of the page to avoid repeated declarations.

Conclusion

This article details three methods for checking collection size with JSTL, analyzing their syntax, version requirements, and applicable scenarios. Developers should select the most suitable implementation based on the specific project environment to ensure code compatibility and maintainability.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.