【问题标题】:How to get the index of forEach loop in integer如何以整数获取forEach循环的索引
【发布时间】:2016-03-24 22:23:56
【问题描述】:

我想以整数形式获取 forEach 循环的索引,我可以以字符串形式获取它,即

<c:forEach var="row" items="${result.rows}" varStatus="loop">
    <c:out value="${loop.index}"/>
</c:forEach>

我想将index的值存储在一个整数变量jsp中。

【问题讨论】:

标签: jsp foreach jstl


【解决方案1】:

只要将它存储在一个变量中,它实际上是一个整数:

<c:set var="index" value="${loop.index}" />
<c:set var="index" value="${index + 1}" />
<c:out value="${index}"/>

应该产生1 2 3等。

【讨论】:

  • 好吧,我想将索引存储在一个可以在jsp中用javascript访问的变量中,我需要将它存储在string或int中,问题是我不知道如何使用带有 javascript 的 jstl 标记。
  • 你要明白JSP是在服务器端执行的。只需像在纯 HTML 中那样编写一个 JavaScript 函数:&lt;script&gt;someFunction(someVariable)&lt;/script&gt; ...您也可以将 someVariable 替换为 EL 表达式以输出索引:&lt;script&gt;someFunction(${index})&lt;/script&gt;,例如,这将导致 &lt;script&gt;someFunction(2)&lt;/script&gt;。跨度>
猜你喜欢
  • 1970-01-01
  • 2015-05-03
  • 2010-11-29
  • 2020-02-25
  • 2013-09-20
  • 1970-01-01
  • 2013-07-18
  • 2014-09-12
  • 2018-04-14
相关资源
最近更新 更多