Use ForEach to Loop Through ArrayList : ForEach « JSTL « Java Tutorial






<%@ page language="java" contentType="text/html" %>
<%@ page import="java.util.*" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>

<%
  // Create an ArrayList with test data
  ArrayList list = new ArrayList();
  Map author1 = new HashMap();
  author1.put("name", "A");
  author1.put("id", new Integer(1));
  list.add(author1);
  Map author2 = new HashMap();
  author2.put("name", "B");
  author2.put("id", new Integer(2));
  list.add(author2);
  Map author3 = new HashMap();
  author3.put("name", "C");
  author3.put("id", new Integer(3));
  list.add(author3);
  pageContext.setAttribute("authors", list);
%>

<html>
  <head>
    <title>Search result: Authors</title>
  </head>
  <body bgcolor="white">
    Here are all authors matching your search critera:
    <table>
      <TH>Name</th>
      <TH>Id</th>
      <c:forEach items="${authors}" var="current">
        <tr>
          <td><c:out value="${current.name}" /><td>
          <td><c:out value="${current.id}" /><td>
        </tr>
      </c:forEach>
    </table>
  </body>
</html>
  Download:  JSTLUseForEachToLoopThroughArrayList.zip( 939 k)








24.7.ForEach
24.7.1.JSTL ForEach Loop
24.7.2.JSTL ForEach Loop with Begin/End Step
24.7.3.JSTL Integer Controlled Loop
24.7.4.Use JSTL ForEach Tag to Loop Through a String
24.7.5.JSTL ForEach Status Count
24.7.6.JSTL ForEach Loop With Step
24.7.7.Reference Array by Index
24.7.8.Use For Each to Loop Through Comma Delimited String
24.7.9.Use ForEach to Loop Through ArrayList
24.7.10.Use JSTL ForEach to Loop through a Vector
24.7.11.Check Even Odd Number in ForEach Loop