Java 如何从jstl的foreach循环中获取索引值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18825950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:25:30  来源:igfitidea点击:

How to get a index value from foreach loop in jstl

javajspfor-loopforeachjstl

提问by Java Questions

I have a value set in the requestobject like the following,

我在request对象中设置了一个值,如下所示,

String[] categoriesList=null;
categoriesList = engine.getCategoryNamesArray();
request.setAttribute("categoriesList", categoriesList );

and this is how I iterate in jsp page

这就是我在jsp页面中迭代的方式

<% if(request.getAttribute("categoriesList") != null) { %>
<c:forEach var="categoryName" items="${categoriesList}">
   <li><a onclick="getCategoryIndex()" href="#">${categoryName}</a></li>
</c:forEach>
<% }%>

How do I get index of each element and pass it to JavaScript function onclick="getCategoryIndex()".

如何获取每个元素的索引并将其传递给 JavaScript 函数onclick="getCategoryIndex()"

采纳答案by newuser

use varStatusto get the index c:forEach varStatus properties

使用varStatus获取索引 c:forEach varStatus 属性

<c:forEach var="categoryName" items="${categoriesList}" varStatus="loop">
    <li><a onclick="getCategoryIndex(${loop.index})" href="#">${categoryName}</a></li>
</c:forEach>

回答by SudoRahul

You can use the varStatusattribute like this:-

您可以varStatus像这样使用属性:-

<c:forEach var="categoryName" items="${categoriesList}" varStatus="myIndex">

myIndex.indexwill give you the index. Here myIndexis a LoopTagStatusobject.

myIndex.index会给你索引。这myIndex是一个LoopTagStatus对象。

Hence, you can send that to your javascript method like this:-

因此,您可以像这样将其发送到您的 javascript 方法:-

<a onclick="getCategoryIndex(${myIndex.index})" href="#">${categoryName}</a>

回答by Laxman G

I face Similar problem now I understand we have some more option : varStatus="loop", Here will be loop will variable which will hold the index of lop.

我面临类似的问题,现在我知道我们有更多的选择:varStatus="loop",这里将是循环变量,它将保存 lop 的索引。

It can use for use to read for Zeor base index or 1 one base index.

它可以用于读取零基索引或 1 一基索引。

${loop.count}` it will give 1 starting base index.

${loop.index} it will give 0 base index as normal Index of arraystart from 0.

${loop.index} it will give 0 base index as normal Index of array从0开始。

For Example :

例如 :

<c:forEach var="currentImage" items="${cityBannerImages}" varStatus="loop">
<picture>
   <source srcset="${currentImage}" media="(min-width: 1000px)"></source>
   <source srcset="${cityMobileImages[loop.count]}" media="(min-width:600px)"></source>
   <img srcset="${cityMobileImages[loop.count]}" alt=""></img>
</picture>
</c:forEach>

For more Info please refer this link

有关更多信息,请参阅此链接

回答by Rakesh Kumar

<a onclick="getCategoryIndex(${myIndex.index})" href="#">${categoryName}</a>

above line was giving me an error. So I wrote down in below way which is working fine for me.

上面那行给了我一个错误。所以我用下面的方式写下了这对我来说很好。

<a onclick="getCategoryIndex('<c:out value="${myIndex.index}"/>')" href="#">${categoryName}</a>

Maybe someone else might get same error. Look at this guys!

也许其他人可能会遇到同样的错误。看看这些家伙!

回答by Pradeep

This works for me:

这对我有用:

<c:forEach var="i" begin="1970" end="2000">
    <option value="${2000-(i-1970)}">${2000-(i-1970)} 
     </option>
</c:forEach>