JSTL Print array itemsList

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

                     

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

0


source to share


2 answers


If I understand your question, you have an ArrayList of ArrayLists. JSTL has some rather strange rules for what a valid "collection" is. This was not satisfied according to the JSTL 1.2 spec , so I skipped over to the source code.

forEach can iterate:

  • Array of native or Object types ( Note: includes any generic arrays due to type of erasure);
  • Collection or any subclass;
  • Any Iterator ;
  • Enumeration ;
  • Everything implemented by Map ; or
  • Comma-seragedated values ​​as String . This is deprecated behavior.

Warning: Using iterators and enums in this context is potentially problematic as it changes their state and there is no way to reset them (via JSTL).



Anyway, the code is simple:

<%@ 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 just a collection of dates. If this collection is a property, just replace ${event}

with ${event.dates}

or something else.

+3


source


<%@ 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>

      



+2


source







All Articles