如何将下面的for
循环转换为jstl
foreach
:
for(int i = 0 ; i<=21; i+=3){
// print foo
}
这就是我目前所拥有的:
<c:forEach varStatus="loop" begin="0" end="21">
// display foo
</c:forEach>
根据jstl,您应该尝试:
<c:forEach begin="0" end="21" step="3" varStatus="loop">
<c:out value="${loop.count}"/>
</c:forEach>
您可以使用jstl步骤属性
<c:forEach varStatus="loop" begin="0" end="21" step="3">
// display foo
</c:forEach>
JSTL教程
`<c:forEach
items="<object>"
begin="<int>"
end="<int>"
step="<int>"
var="<string>"
varStatus="<string>">
</c:forEach>`
项目--循环中要迭代的项目集合
begin--开始迭代的索引。迭代从该属性值中提到的值开始。(如果指定了项目(第一个项目的索引为0。在您的情况下,begin="0">
end--迭代的结束索引。迭代在该属性值(包括该属性值(中提到的值处停止。(如果指定了项目(。在您的情况下,begin="49"。
step--此属性中指定的迭代的步长值。在您的情况下,步骤="3"。
var--包含迭代中当前项的作用域变量的名称。此变量的类型取决于迭代中的项,并且具有嵌套可见性。
varStatus--保存当前迭代循环状态的作用域变量的名称。此变量的类型为javax.servlet.jsp.jst.core.LoopTagStatus,并具有嵌套可见性。
增加3-->步骤="3">
结束49上的循环-->end="49">
链接
此外,如果要使用值本身,可以使用"current"属性。
<c:forEach begin="0" end="2" varStatus="position">
${position.current}
</c:forEach>
这将给出:
01.2
当您使用基于零的数组时,这是非常有用的。