Jsp - How To Banking Enterprise Gibe If Arraylist Is Empty Using Jstl Example
There are multiple ways to cheque if an ArrayList is empty inwards JSP or not. For example, y'all tin exercise the empty operator together with the length component of JSTL to cheque if a listing is empty or not. You tin too exercise scriptlet, which allows y'all to insert Java code into JSP but that is non advisable because it makes it harder to hold a JSP page. You tin too write custom tag but that is non required because JSTL provides y'all necessary support. In this article, I'll demo y'all ii 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 exercise JSTL, the Java touchstone tag library to solve this problem.
The get-go means is to exercise JSTL tag together with empty operator to cheque if an ArrayList is the empty together with minute means is to exercise JSTL function, fn: length() instead of the empty operator equally 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 y'all actually desire to piece of work alongside JSP that is the surface area y'all withdraw to improve, using Java code inwards JSP is non the solution.
but y'all withdraw to import or so other tld file equally shown below
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 position out of characters if a string is passed.
together with hither is the screenshot of the JSP page, when I run it from my Netbeans IDE:
You tin run into that for currency list, which contains INR, USD, together with JPY the empty operator render simulated therefore it is printed equally "no Currency ArrayList is non empty", spell for Fortune500 ArrayList, the empty operator returns true, therefore y'all run into the output "Yes, Fortune500 ArrayList is empty".
That's all most how to cheque if an ArrayList is empty inwards JSTL. It's your alternative to exercise either empty operator or length function, but recall if y'all exercise JSTL function, y'all withdraw to import them using @taglib directory. They conduct hold non imported automatically, non fifty-fifty when y'all import JSTL pith tags similar if, forEach() together with others.
Further Learning
Spring Framework 5: Beginner to Guru
answer)
What is the departure betwixt Servlet together with JSP? (answer)
What is the departure between forward and sendRedirect? (answer)
The existent departure betwixt include together with forwards activity inwards JSP? (answer)
Can y'all declare constructor within Servlet? (answer)
The existent departure betwixt constructor together with init() method inwards Servlet? (answer)
The Java Programming Interview Exposed (book)
Thanks for reading this article so far. If y'all similar this tutorial so delight part alongside your friends together with colleagues. If y'all conduct hold questions so delight drib a banking venture complaint together with I'll endeavor to uncovering an reply for you. If y'all whatsoever other interesting JSP question, experience gratis to share.
The get-go means is to exercise JSTL
Solution 1 - Use empty Operator
The easiest means to uncovering if a listing is empty or non is past times using the empty operator alongside the if tag of JSTL. This operator returns truthful if the listing is empty, but y'all tin too exercise it seek if String, array, or Map is empty equally shown below:<c:if test="${empty listOfCurrency}">
You tin acquire 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 fabric to original this primal concept inwards JSP development.The empty operator render truthful if the operand is null, an empty String, empty array, empty Map, or empty List; false, otherwise.The alone work alongside using the empty operator is that it's non real intuitive, so until y'all know the exact syntax y'all cannot exercise it past times but guessing. For example, next code volition non piece of work inwards JSP, fifty-fifty if y'all exercise JSP:<c:if test="${listOfCurrency.size() == 0 }"> <c:if test="${listOfCurrency.size == 0 }"><c:if test="${listOfCurrency.length == 0 }">
Solution 2 - Use fn:length() function
Another means to cheque if an ArrayList is empty or non is past times using JSTL's length() function, equally shown below:<c:if test="${fn:length(list) > 0}">
but y'all withdraw to import or so other tld file equally 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 position out of characters if a string is passed.
Sample JSP to Check if ArrayList is Empty
Here is the JSP page which demonstrates how y'all tin exercise both empty operator together with length() component 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 alone for testing List<String> currencies = Arrays.asList("USD", "AUD", "INR", "JPY"); List<String> fortune500 = new ArrayList<String>(); // position the List equally 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:
You tin run into that for currency list, which contains INR, USD, together with JPY the empty operator render simulated therefore it is printed equally "no Currency ArrayList is non empty", spell for Fortune500 ArrayList, the empty operator returns true, therefore y'all run into the output "Yes, Fortune500 ArrayList is empty".
That's all most how to cheque if an ArrayList is empty inwards JSTL. It's your alternative to exercise either empty operator or length function, but recall if y'all exercise JSTL function, y'all withdraw to import them using @taglib directory. They conduct hold non imported automatically, non fifty-fifty when y'all import JSTL pith tags similar if, forEach() together with others.
Further Learning
Spring Framework 5: Beginner to Guru
answer)
What is the departure betwixt Servlet together with JSP? (answer)
What is the departure between forward and sendRedirect? (answer)
The existent departure betwixt include together with forwards activity inwards JSP? (answer)
Can y'all declare constructor within Servlet? (answer)
The existent departure betwixt constructor together with init() method inwards Servlet? (answer)
The Java Programming Interview Exposed (book)
Thanks for reading this article so far. If y'all similar this tutorial so delight part alongside your friends together with colleagues. If y'all conduct hold questions so delight drib a banking venture complaint together with I'll endeavor to uncovering an reply for you. If y'all whatsoever other interesting JSP question, experience gratis to share.
Belum ada Komentar untuk "Jsp - How To Banking Enterprise Gibe If Arraylist Is Empty Using Jstl Example"
Posting Komentar