JSTL Size Of List / Array

JSTL Size Of List / Array Example explains about how to check whether JSTL List / Array is empty or not

Consider the example here, a JSP page, which contains an ArrayList wihich contains lot of string values and we need to find whether ArrayList is empty or not

By using 'empty' operator is your best tool for making comparison to 0. so that you can identify whether the ArrayList is empty or not

When you need to find the size of an ArrayList, the JSTL function fn:length is your best fit. Please check fn:length() JSTL Function for finding the Length / Size of List / Array

you can see the below example demonstrating the usage of 'empty' operator inside JSTL
 

Required Libraries

You need to download

  1. Tomcat 9
  2. JSTL 1.2

Following jar must be in classpath

  1. jstl-1.2.jar

JSTL Size Of List / Array

// Check JSTL List Size
    

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page import="java.util.ArrayList"%>
<%
    ArrayList<String> numList = new ArrayList<String>();
    numList.add("one");
    numList.add("two");
    numList.add("three");
    request.setAttribute("numList", numList);
%>
<html>
<body>
    <c:if test="${ empty( numList ) }">
        empty
    </c:if>
    <c:if test="${! empty( numList ) }">
        not empty
    </c:if>
</body>
</html>
Output
not empty

 











Your email address will not be published. Required fields are marked *