【问题标题】:Get the Object Index From ArrayList with JSTL使用 JSTL 从 ArrayList 获取对象索引
【发布时间】:2012-01-05 08:09:50
【问题描述】:

是否可以使用 JSTL 从数组列表中获取“行”索引?

<c:forEach items="${searchResults}" var="contact">
<div style="padding: 5px;">
${contact.firstName} ${contact.lastName}
<br>
${contact.primaryPhone}
</div>
</c:forEach>

我的目的是在每一行设置一个包含每个项目 ID 的超链接,以便用户可以单击并显示一个弹出窗口或另一个页面,并轻松地从 arraylist 中检索单个对象,而无需返回数据库并设置另一个会话对象等

【问题讨论】:

    标签: jsp arraylist jstl


    【解决方案1】:

    使用varStatus 属性。

    <c:forEach items="${searchResults}" var="contact" varStatus="loop">
      <div style="padding: 5px;">
      ${loop.index} - 
      ${contact.firstName} ${contact.lastName}
      <br>
      ${contact.primaryPhone}
     </div>
    </c:forEach>
    

    【讨论】:

      【解决方案2】:

      想出了一个解决方案:

      <c:set var="index" value="${0}"></c:set>
      <c:forEach items="${searchResults}" var="contact">
      <div style="padding: 5px;">
      ${contact.firstName} ${contact.lastName}
      <br>
      ${contact.primaryPhone}
      <br>
      ${index}
      </div>
      <c:set var="index" value="${index+1}"></c:set>
      </c:forEach>
      

      如果有人知道更优雅的方法,我会很高兴看到它。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-12-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-05
        • 1970-01-01
        相关资源
        最近更新 更多