Jsp - How To Depository Fiscal Establishment Gibe If Arraylist Is Empty Using Jstl Example

There are multiple ways to banking enterprise correspond if an ArrayList is empty inwards JSP or not. For example, you lot tin usage the empty operator in addition to the length business office of JSTL to banking enterprise correspond if a listing is empty or not. You tin too usage 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 too write custom tag but that is non required because JSTL provides you lot necessary support. In this article, I'll demo you lot ii ways to banking enterprise correspond if a given listing or ArrayList is empty or non inwards JSP page without using the scriptlet. You guessed it right, nosotros volition usage JSTL, the Java criterion tag library to solve this problem.

The start agency is to usage JSTL tag in addition to empty operator to banking enterprise correspond if an ArrayList is the empty in addition to minute agency is to usage 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 withdraw to improve, using Java code inwards JSP is non the solution.


Solution 1 - Use empty Operator

The easiest agency to honour 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 too usage it evidence 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 exclusively employment amongst using the empty operator is that it's non really intuitive, so until you lot know the exact syntax you lot cannot usage it past times simply guessing. For example, next code volition non piece of work inwards JSP, fifty-fifty if you lot usage JSP:
<c:if test="${listOfCurrency.size() == 0 }">      <c:if test="${listOfCurrency.size == 0 }">   
<c:if test="${listOfCurrency.length == 0 }">   
You tin acquire to a greater extent than most the empty operator in addition to if tag of JSTL inwards the shape Head First Servlet in addition to JSP book. Their chapter on JSTL in addition to Custom tag is the best cloth to master copy this telephone substitution concept inwards JSP development.


Solution 2 - Use fn:length() function

Another agency to banking enterprise correspond 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 withdraw to import roughly 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 in addition to returns an int which represents the size of the collection or array if a collection is passed in addition to the set out 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 usage both empty operator in addition to length() business office to banking enterprise correspond 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 enterprise correspond if ArrayList is empty inwards JSP?</title>     </head>      <body>           <h2> 2 ways to banking enterprise correspond 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 exclusively 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 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> 


in addition to hither is the screenshot of the JSP page, when I run it from my Netbeans IDE:


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

You tin run into that for currency list, which contains INR, USD, in addition to JPY the empty operator render imitation thus it is printed every bit "no Currency ArrayList is non empty", piece for Fortune500 ArrayList, the empty operator returns true, thus you lot run into the output "Yes, Fortune500 ArrayList is empty".


That's all most how to banking enterprise correspond if an ArrayList is empty inwards JSTL. It's your selection to usage either empty operator or length function, but shout back if you lot usage JSTL function, you lot withdraw to import them using @taglib directory. They conduct hold non imported automatically, non fifty-fifty when you lot import JSTL heart tags similar if, forEach() in addition to others.

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

Thanks for reading this article so far. If you lot similar this tutorial in addition to so delight portion amongst your friends in addition to colleagues. If you lot conduct hold questions in addition to so delight drib a banknote in addition to I'll endeavor to honour an respond for you. If you lot whatsoever other interesting JSP question, experience costless to share.

Komentar

Postingan populer dari blog ini

2 Ways To Banking Concern Tally If A String Is Rotation Of Other Inward Java?

How To Convert String To Integer To String Inward Coffee Amongst Example

How To Induce Chrome, Firefox Blurry, Over Bright, Fading Afterwards Windows Ten Update