gpt4 book ai didi

java - 如何在 JSTL 中打印出数组对象的索引

转载 作者:行者123 更新时间:2023-12-02 07:30:29 24 4
gpt4 key购买 nike

我在 JSTL 中遇到问题,我有一个来自 servlet 中设置的请求属性的数组对象。我想做的就是打印数组的索引。有什么帮助吗?代码如下:

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="/WEB-INF/tlds/Functions" prefix="func"%>
<table>
<tr>
<td>
<b>k</b> =
</td>
<td>
<table border="1">
<c:forEach var="i" begin="0" end="${responseVector.length}">
<tr>
<td>k<sub>${i}</sub></td><!-- I wish to print the indexes here -->
</tr>
</c:forEach>
</table>
</td>
<td>
<table border="1">
<c:forEach var="i" items="${responseVector}">
<tr>
<td>${func:roundOff(i, 4)}</td>
</tr>
</c:forEach>
</table>
</td>
</tr>
</table>
<br/>

在上面的代码中,responseVector 是一个 double 组对象,但我希望第一个循环中的变量 i 在每次循环迭代时打印数组对象的索引。我的预期输出是:k0,k1,k2,...但我有一个异常(exception)。

最佳答案

试试这个:(使用varStatus属性)

<c:forEach items="${responseVector}" var="r" varStatus="status">
<c:out value="${status.index}"/>
</c:forEach>

${status.index} 将为您提供:

Retrieves the index of the current round of the iteration. If iteration is being performed over a subset of an underlying array, java.lang.Collection, or other type, the index returned is absolute with respect to the underlying collection. Indices are 0-based.

更多信息here .

关于java - 如何在 JSTL 中打印出数组对象的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12983633/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com