JSTL打印arrayList元素

JSTL打印arrayList元素

本文介绍了JSTL打印arrayList元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSP页面,该页面接收事件对象的ArrayList,每个事件对象都包含一个日期的ArrayList.我使用以下命令遍历事件对象:

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:

如何遍历每个事件对象的dateTimes ArrayList并打印出每个事件的日期/时间?

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

推荐答案

如果我理解您的问题,那么您实质上就是一个ArrayLists的ArrayList. JSTL对于有效的项目"集合"有一些相当奇怪的规则. JSTL 1.2规范并没有给出足够的答案,所以我去了到源代码.

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可以迭代:

forEach can iterate over:

  • 本机数组或 Object 类型(注意:由于类型删除,其中包括任何通用数组);
  • 收藏或任何其他子类;
  • 任何迭代器
  • 枚举
  • 任何实现地图的东西;或
  • 以逗号分隔的值作为字符串.此行为已被弃用.
  • 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.

警告语:在这种情况下使用迭代器和枚举可能会带来问题,因为这样做会修改其状态,并且无法(通过JSTL)重置它们.

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>

假设事件对象仅仅是日期的集合.如果该集合是一个属性,则只需将${event}替换为${event.dates}或其他任何内容.

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.

这篇关于JSTL打印arrayList元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 06:03