Java 使用 JSTL forEach 循环的 varStatus 作为 ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6600738/
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-16 08:06:36  来源:igfitidea点击:

Use JSTL forEach loop's varStatus as an ID

javajspjstlel

提问by Mark W

I want to use the count from the JSTL forEach loop, but my code doesnt seem to work.

我想使用 JSTL forEach 循环中的计数,但我的代码似乎不起作用。

<c:forEach items="${loopableObject}" var="theObject" varStatus="theCount">
    <div id="divIDNo${theCount}">
    </div>
</c:forEach>

produces

产生

<div id="divIDNojavax.servlet.jsp.jstl.core.LoopTagSupportStatus@5570e2" >

采纳答案by highlycaffeinated

The variable set by varStatusis a LoopTagStatusobject, not an int. Use:

变量 set byvarStatus是一个LoopTagStatus对象,而不是一个 int。用:

<div id="divIDNo${theCount.index}">

To clarify:

澄清:

  • ${theCount.index}starts counting at 0unless you've set the beginattribute
  • ${theCount.count}starts counting at 1
  • ${theCount.index}0除非您设置了begin属性,否则开始计数
  • ${theCount.count}开始计数 1

回答by Nathanphan

You can try this. similar result

你可以试试这个。类似的结果

 <c:forEach items="${loopableObject}" var="theObject" varStatus="theCount">
    <div id="divIDNo${theCount.count}"></div>
 </c:forEach>

回答by jyoti pani

Its really helped me to dynamically generate ids of showDetailItemfor the below code.

它确实帮助我showDetailItem为以下代码动态生成 id 。

<af:forEach id="fe1" items="#{viewScope.bean.tranTypeList}" var="ttf" varStatus="ttfVs" > 
<af:showDetailItem  id ="divIDNo${ttfVs.count}" text="#{ttf.trandef}"......>

if you execute this line <af:outputText value="#{ttfVs}"/>prints the below:

如果您执行此行,则会<af:outputText value="#{ttfVs}"/>打印以下内容:

{index=3, count=4, last=false, first=false, end=8, step=1, begin=0}

{index=3, count=4, last=false, first=false, end=8, step=1, begin=0}

回答by diego matos - keke

you'd use any of these:

你会使用以下任何一种:

JSTL c:forEach varStatus properties

JSTL c:forEach varStatus 属性

Property Getter Description

属性获取器描述

  • current getCurrent() The item (from the collection) for the current round of iteration.

  • index getIndex() The zero-based index for the current round of iteration.

  • count getCount() The one-based count for the current round of iteration

  • first isFirst() Flag indicating whether the current round is the first pass through the iteration
  • last isLast() Flag indicating whether the current round is the last pass through the iteration

  • begin getBegin() The value of the begin attribute

  • end getEnd() The value of the end attribute

  • step getStep() The value of the step attribute

  • current getCurrent() 当前轮次迭代的项目(来自集合)。

  • index getIndex() 当前轮次迭代的从零开始的索引。

  • count getCount() 当前一轮迭代的从一开始的计数

  • first isFirst() 指示当前轮次是否为第一遍迭代的标志
  • last isLast() 指示当前轮次是否为最后一次迭代的标志

  • begin getBegin() begin 属性的值

  • end getEnd() 结束属性的值

  • step getStep() step属性的值