How To Loop Over Hashmap Inwards Jsp Using Jstl
Though in that place are a release of ways to loop over an HashMap inwards JSP, or whatever other Map implementation e.g. Hashtable, I personally prefer the JSTL foreach tag for iteration over HashMap inwards JSP. As a Java programmer, you lot volition oftentimes bring a rigid urge to work Java code straight inwards JSP using scriptlet, but that's a bad coding exercise together with i should ever avoid that. In fact, past times smart work of aspect linguistic communication together with JSTL heart together with soul tag library, you lot tin trim a lot of Java code from your JSP page. In our terminal post, nosotros bring seen an instance of JSTL foreach tag to loop over List, but non an HashMap, together with that creates a uncertainty inwards i of my readers hear that foreach tag doesn't back upwards Map implementation similar HashMap or Hashtable every bit they are non Collection, but that's non true. You tin work the same technique to loop over an HashMap inwards JSP which nosotros bring used before to loop over a listing inwards JSP.
The JSTL foreach tag has exceptional back upwards for looping over Map, it provides you lot both cardinal together with value past times using var attribute. In the instance of HashMap, object exported using var contains Map.Entry object.
Since Map.Entry has getKey() together with getValue() method, you lot tin access them using aspect linguistic communication $(entry.key) together with $(entry.value), every bit nosotros volition seen inwards our example. You tin iterate over HashMap to do a tabular array of cardinal together with value, or whatever HTML chemical component subdivision e.g. <select>, which needs text together with value.</select>
cardinal = $(entry.key} together with value = $(entry.value}
The JSTL foreach tag has exceptional back upwards for looping over Map, it provides you lot both cardinal together with value past times using var attribute. In the instance of HashMap, object exported using var contains Map.Entry object.
Since Map.Entry has getKey() together with getValue() method, you lot tin access them using aspect linguistic communication $(entry.key) together with $(entry.value), every bit nosotros volition seen inwards our example. You tin iterate over HashMap to do a tabular array of cardinal together with value, or whatever HTML chemical component subdivision e.g. <select>, which needs text together with value.</select>
Looping over HashMap inwards JSP using JSTL
When nosotros loop over an HashMap using JSTL foreach tag, it stores the electrical flow Map.Entry object into variable exported past times var attribute of foreach tag. If var="entry" thence $(entry.key} volition give us cardinal together with $(entry.value) volition render value, every bit shown inwards below example:cardinal = $(entry.key} together with value = $(entry.value}
Just remember, In lodge to work JSTL heart together with soul tag library, you lot necessitate to include jstl.jar file inwards the classpath. This commonly agency copying jstl.jar into WEB-INF/lib folder of your projection together with import them using the taglib directive every bit <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>.
If you lot are non certain how classpath industrial plant inwards Java e.g. how just Servlet together with JSP await for a cast file inwards WEF-INF together with META-INF folder, I advise you lot reading the custom tag chapter from the Head First Servlet together with JSP. It has explained this concept real well.
Iterating a HashMap inwards JSP using JSTL foreach loop
Let's run across a fully functional, code instance of looping over HashMap inwards JSP. In this example, you lot volition dynamically do a tabular array amongst 2 columns, i for cardinal together with other for value, past times using information stored inwards HashMap.Though I bring used scriptlet for embedding Java code into JSP for populating HashMap, you lot should non do that, it's only for demonstration purpose. In fact, the whole indicate of using JSTL together with aspect linguistic communication is to avoid Java code inwards JSP, every bit Java code inwards JSP leads to a maintenance nightmare.
You tin farther read Murach's Java Servlets together with JSP to larn to a greater extent than close advanced best practices to follow piece developing a spider web application inwards Java JEE platform.
Anyway, hither is our sample JSP page
<%@page import="java.util.Hashtable"%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <%@page import="java.util.Map"%> <%@page import="java.util.HashMap"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <!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 Loop over Map i.e. HashMap or Hashtable inwards JSP - JSTL foreach tag example</title> </head> <body> <h2> How to traverse HashMap inwards JSP</h2> <% // Avoid Java Code inwards JSP - This is alone for repose of testing Map<Integer, String> numberToString = new HashMap<Integer, String>(); numberToString.put(1, "JSP"); numberToString.put(2, "Java"); numberToString.put(3, "JSTL"); numberToString.put(4, "J2EE"); numberToString.put(5, "JEE"); // lay the hashmap every bit pageContext attribute pageContext.setAttribute("map", numberToString); %> <%-- JSTL foreach tag instance to loop a HashMap inwards JSP --%> <table> <c:forEach var="entry" items="${pageScope.map}"> <tr><td><c:out value="${entry.key}"/></td> <td><c:out value="${entry.value}"/> </td></tr> </c:forEach> </table> <h2> How to loop Hashtable inwards JSP</h2> <% // Avoid Java Code inwards JSP - This is alone for repose of testing Map<String, Integer> prices = new Hashtable<String, Integer>(); prices.put("Google", 500); prices.put("Apple", 300); prices.put("Amazon", 320); prices.put("BABA", 94); prices.put("MSFT", 30); // putting hashtable into pageContext variable pageContext.setAttribute("sharePrice", prices); %> <%-- JSTL foreach tag instance to loop Hashtable inwards JSP --%> <table> <c:forEach var="entry" items="${pageScope.sharePrice}"> <tr><td><c:out value="${entry.key}"/></td> <td><c:out value="${entry.value}"/> </td></tr> </c:forEach> </table> </body> </html> Output: How to traverse HashMap inwards JSP 1 JSP 2 Java three JSTL iv J2EE v JEE
Here is the actual HTML page created past times this JSP and how it volition await similar when you lot run your spider web application or deploy it on tomcat.
That's all on How to loop or iterate a HashMap inwards JSP using JSTL foreach tag. JSTL heart together with soul tag is a powerful tag together with it non alone supports iteration of Map e.g.. HashMap or Hashtable, but besides whatever Collection cast including List, Set together with array. It's JSP best exercise to work the JSTL together with custom tags for all traversal, iteration, together with looping needs, together with endeavour to avoid using Java code inwards JSP every bit much every bit possible.
Further Learning
Spring Framework 5: Beginner to Guru
5 Examples of JSTL Core If tag
0 Response to "How To Loop Over Hashmap Inwards Jsp Using Jstl"
Post a Comment