Thursday, May 10, 2012

JSTL forEach loop to iterate List, Map, Map of List, List of Map

Lets see how we can use JSTL to iterate List, Map, Map of List and List of Map. Below are the tested JSP codes, directly you can run below JSPs and see the outputs.

Note : The JSTL tag library needs jstl-1.2.jar in application's classpath. To download jstl-1.2.jar, click here

1. List

Lets see how to iterate List using JSTL forEach loop. Using below code you can iterate ArrayList, Vector or LinkedList.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page import="java.util.ArrayList"%>
<%
    ArrayList<String> list = new ArrayList<String>();
    list.add("val1");
    list.add("val2");
    list.add("val3");
    
    request.setAttribute("MyList", list);
%>
<html>
<body>
    <c:forEach items="${MyList}" var="listItem">
        ${listItem} <br/>
    </c:forEach>
</body>
</html>


2. Map

Iterate Maps like HashMap, Hashtable, TreeMap or LinkedHashMap.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page import="java.util.Hashtable"%>
<%
    Hashtable<String,String> map = new Hashtable<String,String>();
  
 map.put("key1", "val1");
 map.put("key2", "val2");
 map.put("key3", "val3");
    
    request.setAttribute("MyMap", map);
%>
<html>
<body>
    <c:forEach items="${MyMap}" var="mapItem">
        ${mapItem.key} ${mapItem.value} <br/>
    </c:forEach>
</body>
</html>

3. List of Map

How to Iterate List of Map ?

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page import="java.util.ArrayList"%>
<%@ page import="java.util.Hashtable"%>
<%
    ArrayList<Hashtable<String,String>> listOfMap = new ArrayList<Hashtable<String,String>>();
  
 Hashtable<String,String> map;
 
 for(int i=0;i<3;i++){
  map = new Hashtable<String, String>();
  
  map.put("k1", "value1"+i);
  map.put("k2", "value2"+i);
  
  listOfMap.add(map);
 }
    
    request.setAttribute("listOfMap", listOfMap);
%>
<html>
<body>
    <c:forEach items="${listOfMap}" var="maps">
        <c:forEach items="${maps}" var="mapItem">
      ${mapItem.key} ${mapItem.value} <br/>
   </c:forEach>
    </c:forEach>
</body>
</html>

4. Map of List

Iterate Map of List

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page import="java.util.ArrayList"%>
<%@ page import="java.util.Hashtable"%>
<%
    Hashtable<String,ArrayList<String>> mapOfList = new Hashtable<String,ArrayList<String>>(); 
  
 ArrayList<String> list;
 
 for(int i=0;i<3;i++){
  list = new ArrayList<String>();
  
  list.add("value1");
  list.add("value2");
  
  mapOfList.put("key"+i,list);
 }
    
    request.setAttribute("mapOfList", mapOfList);
%>
<html>
<body>
    <c:forEach items="${mapOfList}" var="list">
        <c:forEach items="${list.value}" var="listItem">
       ${listItem} <br/>
    </c:forEach>
    </c:forEach>
</body>
</html>

Note: You may get below exceptions if you do not provide proper value in items section.

Stacktrace:] with root cause
javax.servlet.jsp.JspTagException: Don't know how to iterate over supplied "items" in <forEach>
 at org.apache.taglibs.standard.tag.common.core.ForEachSupport.toForEachIterator(ForEachSupport.java:274)
 at org.apache.taglibs.standard.tag.common.core.ForEachSupport.supportedTypeForEachIterator(ForEachSupport.java:238)
 at org.apache.taglibs.standard.tag.common.core.ForEachSupport.prepare(ForEachSupport.java:155)
 at javax.servlet.jsp.jstl.core.LoopTagSupport.doStartTag(LoopTagSupport.java:256)
 at org.apache.jsp.test_jsp._jspx_meth_c_005fforEach_005f1(test_jsp.java:156)
 at org.apache.jsp.test_jsp._jspx_meth_c_005fforEach_005f0(test_jsp.java:119)
 at org.apache.jsp.test_jsp._jspService(test_jsp.java:83)
 at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
 at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
 at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
 at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
 at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:498)
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
 at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:394)
 at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)
 at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
 at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)
 at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
 at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
 at java.lang.Thread.run(Thread.java:619)

1 comment: