Java for 循环到 jstl forEach
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19788158/
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
for loop to jstl forEach
提问by tokhi
How to convert below for
loop to a jstl
foreach
:
如何将下面的for
循环转换为一个jstl
foreach
:
for(int i = 0 ; i<=21; i+=3){
// print foo
}
This is what I have so far:
这是我到目前为止:
<c:forEach varStatus="loop" begin="0" end="21">
// display foo
</c:forEach>
采纳答案by user987339
回答by SpringLearner
you can use jstl step attribute
您可以使用 jstl 步骤属性
<c:forEach varStatus="loop" begin="0" end="21" step="3">
// display foo
</c:forEach>
回答by Abin Oommen George
`<c:forEach
items="<object>"
begin="<int>"
end="<int>"
step="<int>"
var="<string>"
varStatus="<string>">
</c:forEach>`
items-- Collection of items to iterate in the loop
items-- 要在循环中迭代的项目集合
begin-- Begin index of the iteration. Iteration begins at the value mentioned in this attribute value. (if items specified) First item has index of 0.In your case begin="0"
begin-- 迭代的开始索引。迭代从该属性值中提到的值开始。(如果指定了项目)第一个项目的索引为 0。在您的情况下 begin="0"
end-- End index of the iteration. Iteration stops at the value mentioned in this attribute value (inclusive). (if items specified).In your case begin="49".
end-- 迭代的结束索引。迭代在此属性值(含)中提到的值处停止。(如果指定了项目)。在你的情况下开始=“49”。
step-- Step value for the iteration specified in this attribute.In your case step="3".
step-- 此属性中指定的迭代的步长值。在您的情况下,step="3"。
var-- Name of the scoped variable which holds the current item in the iteration. This variable's type depends on the items in the iteration and has nested visibility.
var-- 在迭代中保存当前项目的作用域变量的名称。此变量的类型取决于迭代中的项目并具有嵌套可见性。
varStatus-- Name of the scoped variable which holds the loop status of the current iteration. This variable is of type javax.servlet.jsp.jstl.core.LoopTagStatus and has nested visibility.
varStatus-- 保存当前迭代循环状态的作用域变量的名称。此变量的类型为 javax.servlet.jsp.jstl.core.LoopTagStatus 并具有嵌套可见性。
to increment by 3 -->step="3"
增加 3 -->step="3"
end loop on 49 -->end="49"
在 49 上结束循环 -->end="49"
回答by Dakos
Also if you want to use the value itself, you can use the 'current' attribute.
此外,如果您想使用值本身,您可以使用 'current' 属性。
<c:forEach begin="0" end="2" varStatus="position">
${position.current}
</c:forEach>
this will give:
这将给出:
0 1 2
0 1 2
this is usefull when you are working with arrays which are zero-based.
当您使用从零开始的数组时,这很有用。