是否可以在jstl中使用foreach同时迭代两个项目? [英] is it possible to iterate two items simultaneously using foreach in jstl?

查看:30
本文介绍了是否可以在jstl中使用foreach同时迭代两个项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的模型中有两个项目,我想使用 jstl foreach 同时迭代它们.如何使用正确的语法实现这一点?

I have two items from my model and I want to iterate them at the same using jstl foreach. how can I achieve this using a correct syntax?

推荐答案

可以调用 varStatus.index 来获取本轮迭代的索引,然后用它来查找第二个名单.

You can call varStatus.index to get the index of the current round of iteration, and then use it as a lookup for the second list.

例如,如果你有两个列表 people.firstnamespeople.lastnames 你可以这样做:

For example, if you have two lists people.firstnames and people.lastnames you can do:

<c:forEach var="p" items="${people.firstnames}" varStatus="status">
  <tr>
      <td>${p}</td>
      <td>${people.lastnames[status.index]}</td>
  </tr>
</c:forEach>

这篇关于是否可以在jstl中使用foreach同时迭代两个项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆