Jsp - How To Cheque If Arraylist Is Empty Using Jstl Example

There are multiple ways to cheque if an ArrayList is empty inwards JSP or not. For example, you lot tin give notice utilization the empty operator together with the length business office of JSTL to cheque if a listing is empty or not. You tin give notice every bit good utilization scriptlet, which allows you lot to insert Java code into JSP but that is non advisable because it makes it harder to keep a JSP page. You tin give notice every bit good write custom tag but that is non required because JSTL provides you lot necessary support. In this article, I'll present you lot 2 ways to cheque if a given listing or ArrayList is empty or non inwards JSP page without using the scriptlet. You guessed it right, nosotros volition utilization JSTL, the Java criterion tag library to solve this problem.

The commencement agency is to utilization JSTL tag together with empty operator to cheque if an ArrayList is the empty together with 2nd agency is to utilization JSTL function, fn: length() instead of the empty operator every bit shown inwards our example. Unfortunately, both of these options are non obvious to anyone who is non a power-coder inwards JSP or JSTL, but if you lot actually desire to piece of work amongst JSP that is the expanse you lot involve to improve, using Java code inwards JSP is non the solution.


Solution 1 - Use empty Operator

The easiest agency to uncovering if a listing is empty or non is past times using the empty operator amongst the if tag of JSTL. This operator returns truthful if the listing is empty, but you lot tin give notice every bit good utilization it essay if String, array, or Map is empty every bit shown below:

<c:if test="${empty listOfCurrency}">
The empty operator render truthful if the operand is null, an empty String, empty array, empty Map, or empty List; false, otherwise. 
The entirely work amongst using the empty operator is that it's non really intuitive, so until you lot know the exact syntax you lot cannot utilization it past times only guessing. For example, next code volition non piece of work inwards JSP, fifty-fifty if you lot utilization JSP:
<c:if test="${listOfCurrency.size() == 0 }">      <c:if test="${listOfCurrency.size == 0 }">   
<c:if test="${listOfCurrency.length == 0 }">   
You tin give notice larn to a greater extent than nearly the empty operator together with if tag of JSTL inwards the shape Head First Servlet together with JSP book. Their chapter on JSTL together with Custom tag is the best fabric to original this primal concept inwards JSP development.


Solution 2 - Use fn:length() function

Another agency to cheque if an ArrayList is empty or non is past times using JSTL's length() function, every bit shown below:

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

but you lot involve to import around other tld file every bit shown below

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

The fn:length accepts a collection or string together with returns an int which represents the size of the collection or array if a collection is passed together with the give away of characters if a string is passed.



Sample JSP to Check if ArrayList is Empty

Here is the JSP page which demonstrates how you lot tin give notice utilization both empty operator together with length() business office to cheque if an ArrayList is empty or not:

<%@page contentType="text/html" pageEncoding="UTF-8"%>  <%@page import="java.util.List"%> <%@page import="java.util.Arrays"%> <%@page import="java.util.ArrayList"%>  <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"     "http://www.w3.org/TR/html4/loose.dtd"> <html>     <head>         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">         <title> How to cheque if ArrayList is empty inwards JSP?</title>     </head>      <body>           <h2> 2 ways to cheque if ArrayList is empty inwards JSP</h2>         <ol>             <li>By using empty operator</li>             <li>By using fn:length() function</li>         </ol>          <%             // Avoid Java Code inwards JSP - This is entirely for testing             List<String> currencies = Arrays.asList("USD", "AUD", "INR", "JPY");             List<String> fortune500 = new ArrayList<String>();              // set the List every bit pageContext attribute             pageContext.setAttribute("currencies", currencies);             pageContext.setAttribute("fortune500", fortune500);         %>          <h4>Testing if given listing is emtpy using 'empty' operator</h4>          <c:if test="${empty currencies}">             <p>Yes, Currency ArrayList is empty" </p>         </c:if>              <p>No, Currency ArrayList is non empty" </p>          <h5>Testing ane time to a greater extent than if List is empty</h5>          <c:if test="${empty fortune500}">             <p> Yes, Fortune500 ArrayList is empty" <p/>         </c:if>           <h4>Checking if ArrayList is empty using fn:length() function</h4>         <c:if test="${fn:length(fortune500) == 0}">             <p>Yes, ArrayList is empty" <p/>         </c:if>       </body> </html> 


together with hither is the screenshot of the JSP page, when I run it from my Netbeans IDE:


 There are multiple ways to cheque if an ArrayList is empty inwards JSP or non JSP - How to cheque if ArrayList is Empty using JSTL Example

You tin give notice run across that for currency list, which contains INR, USD, together with JPY the empty operator render fake thus it is printed every bit "no Currency ArrayList is non empty", spell for Fortune500 ArrayList, the empty operator returns true, thus you lot run across the output "Yes, Fortune500 ArrayList is empty".


That's all nearly how to cheque if an ArrayList is empty inwards JSTL. It's your choice to utilization either empty operator or length function, but recall if you lot utilization JSTL function, you lot involve to import them using @taglib directory. They receive got non imported automatically, non fifty-fifty when you lot import JSTL nub tags similar if, forEach() together with others.

Further Learning
Spring Framework 5: Beginner to Guru
answer)
What is the divergence betwixt Servlet together with JSP? (answer)
What is the divergence between forward and sendRedirect? (answer)
The existent divergence betwixt include together with forwards activeness inwards JSP? (answer)
Can you lot declare constructor within Servlet? (answer)
The existent divergence betwixt constructor together with init() method inwards Servlet? (answer)
The Java Programming Interview Exposed (book)

Thanks for reading this article so far. If you lot similar this tutorial so delight portion amongst your friends together with colleagues. If you lot receive got questions so delight driblet a banking corporation complaint together with I'll seek to uncovering an reply for you. If you lot whatever other interesting JSP question, experience costless to share.

0 Response to "Jsp - How To Cheque If Arraylist Is Empty Using Jstl Example"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel