views:

2217

answers:

2

I have a JSP page that receives an ArrayList of event objects, each event object contains an ArrayList of dates. I am iterating through the event objects with the following:

How can I iterate through the dateTimes ArrayList of each event object and print out each events date/times ?

+2  A: 

If I understand your question, you essentially have an ArrayList of ArrayLists. JSTL has some fairly odd rules for what a valid "items" "collection" is. This wasn't sufficiently answered by the JSTL 1.2 specification so I went to the source code.

forEach can iterate over:

  • An array of native or Object types (Note: this includes any generic arrays because of type erasure);
  • Collection or any subclass;
  • Any Iterator;
  • An Enumeration;
  • Anything implementing Map; or
  • Comma-serparated values as a String. This is deprecated behaviour.

Word of caution: use of Iterators and Enumerations in this context is potentially problematic as doing so modifies their state and there's no way to reset them (via JSTL).

Anyway, the code is straightforward:

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt" %>
<c:forEach var="event" items="${events}">
  <c:forEach var="date" items="${event}">
    <fmt:formatDate value="${date}" type="both"
      timeStyle="long" dateStyle="long" />
  </c:forEach>
</c:forEach>

Assuming the event object is merely a collection of dates. If that collection is a property then just replace ${event} with ${event.dates} or whatever.

cletus
at least for jsp 2.0, it is not enough to implement iterable
krosenvold
You are correct. Edited, corrected and clarified.
cletus
+2  A: 
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt" %>
 <c:forEach items="${events}" var="event">
     <c:forEach items="${event.dates}" var="date">
          <fmt:formatDate value="${date}" type="both"
          timeStyle="long" dateStyle="long" />
      </c:forEach>
 </c:forEach>
krosenvold