JavaServer Tag library is one of the most used JSP tag library out there. I have used it almost in all of my JEE based projects. The best feature probably is the Iterator API in JSTL tag library. Here is a small code snippet which you might not know. Its very easy to iterate Lists using JSTL. For example:
//Java
List<String> cityList = new ArrayList<String>();
cityList.add("Washington DC");
cityList.add("Delhi");
cityList.add("Berlin");
cityList.add("Paris");
cityList.add("Rome");
request.setAttribute("cityList", cityList);
//JSP
<c:forEach var="city" items="cityList">
<b> ${city} </b>
</c:forEach>
Code language: Java (java)
But what if you want to iterate a Hashmap? Well that too is piece of cake. Here is the example://Java
Map<String, String> countryCapitalList = new HashMap<String, String>();
countryCapitalList.put("United States", "Washington DC");
countryCapitalList.put("India", "Delhi");
countryCapitalList.put("Germany", "Berlin");
countryCapitalList.put("France", "Paris");
countryCapitalList.put("Italy", "Rome");
request.setAttribute("capitalList", countryCapitalList);
//JSP
<c:forEach var="country" items="${capitalList}">
Country: ${country.key} - Capital: ${country.value}
</c:forEach>
Code language: Java (java)
Happy iterating :-)
OOH that’s great !!! surely this code will help me a lot … thanks
What if you use in the jsp file? How will the code be altered then and how does that work…..?
What I meant was this:
What if you use jsp:useBean in the jsp file? How will the code be altered then and how does that work…..?
in first foreach loop u missed to items=”${cityList}” k
I attempted using your hashmap example. In the jsp
//JSP
Country: ${entry.key} – Capital: ${entry.value}
However this does not work.
@Gura : add in jsp and try if it works
sorry i missed code —- add
Thanks
Thanks man..your blogs helped me a lot..thank you once again..
Thank you for this info! But you can actually get access to the value by just using the key, like if it was an array:
This is my DAO
Insert title here
click here to see statement:
accn
description
date
Here is my jsp
But i am getting an exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /stmt.jsp at line 19
16:
17:
18: accn
19:
20:
21:
22:
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:401)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause
javax.servlet.ServletException: javax.servlet.jsp.JspTagException: Don’t know how to iterate over supplied “items” in <forEach>
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:862)
Please help me
Hi, it seems that you are using jetty, I propose you to switch to tomcat :) or move scriplet code in controller / servlet
Thanks…
If that Map is present in one method like below.
how to get the values key and value to a select box…?
Hi All,
can any one please help me out that are there any chances were in we can remove hash map key/value pair through JSTL
below is the piece of code,
${a.key}
now we have got the MAP key, can we remove this key from existing Map
Thanks
Daya
Is it possible to iterate over arrays?
thank you,but my map value is Object type..could you please tell me how can i iterate my object values in jsp
Thanks, nice tips
Iterating through map is bugged. It iterates successfully if there is only 1 static map. However, if map changes dynamically, let’s say by selecting an item from dropdown menu, a new map is generated for iteration, then c:foreach tries to use the first map for iteration which results in incorrect data..
Nice Page