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

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

The showtime agency is to exercise JSTL tag together with empty operator to banking concern gibe if an ArrayList is the empty together with instant agency is to exercise 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 yous actually desire to piece of work alongside JSP that is the expanse yous necessitate to improve, using Java code inwards JSP is non the solution.


Solution 1 - Use empty Operator

The easiest agency to detect if a listing is empty or non is yesteryear using the empty operator alongside the if tag of JSTL. This operator returns truthful if the listing is empty, but yous tin dismiss every bit good exercise 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 alone occupation alongside using the empty operator is that it's non really intuitive, so until yous know the exact syntax yous cannot exercise it yesteryear simply guessing. For example, next code volition non piece of work inwards JSP, fifty-fifty if yous exercise JSP:
<c:if test="${listOfCurrency.size() == 0 }">      <c:if test="${listOfCurrency.size == 0 }">   
<c:if test="${listOfCurrency.length == 0 }">   
You tin dismiss larn to a greater extent than most 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 textile to original this primal concept inwards JSP development.


Solution 2 - Use fn:length() function

Another agency to banking concern gibe if an ArrayList is empty or non is yesteryear using JSTL's length() function, every bit shown below:

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

but yous necessitate to import closed to 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 divulge of characters if a string is passed.



Sample JSP to Check if ArrayList is Empty

Here is the JSP page which demonstrates how yous tin dismiss exercise both empty operator together with length() business office to banking concern gibe 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 banking concern gibe if ArrayList is empty inwards JSP?</title>     </head>      <body>           <h2> 2 ways to banking concern gibe 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 alone for testing             List<String> currencies = Arrays.asList("USD", "AUD", "INR", "JPY");             List<String> fortune500 = new ArrayList<String>();              // seat 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 over again 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 banking concern gibe if an ArrayList is empty inwards JSP or non JSP - How to banking concern gibe if ArrayList is Empty using JSTL Example

You tin dismiss come 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", land for Fortune500 ArrayList, the empty operator returns true, thus yous come across the output "Yes, Fortune500 ArrayList is empty".


That's all most how to banking concern gibe if an ArrayList is empty inwards JSTL. It's your selection to exercise either empty operator or length function, but recall if yous exercise JSTL function, yous necessitate to import them using @taglib directory. They receive got non imported automatically, non fifty-fifty when yous import JSTL substance tags similar if, forEach() together with others.

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

Thanks for reading this article so far. If yous similar this tutorial together with then delight percentage alongside your friends together with colleagues. If yous receive got questions together with then delight drib a complaint together with I'll endeavor to detect an reply for you. If yous whatsoever other interesting JSP question, experience gratis to share.

Sumber https://javarevisited.blogspot.com/

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