问题
I have to iterate a collections in JSTL, but I would like the index to be incremented +3 each loop, something like
for (int i=0; i<50; i+=3) {
}
but in JSTL
回答1:
use <c:forEach/>
tag which exist as an alternative for while
, do-while
and for Loop in jstl
via scriptlet
<c:forEach var="i" begin="0" end="50" step="3" >
<c:out value="${i}"/>
</c:forEach>
begin
for initialization, end
for termination and step
for increment
回答2:
`<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
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"
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".
step -- Step value for the iteration specified in this attribute.In your case 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.
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.
to increment by 3 --> step="3"
end loop on 49 --> end="49"
来源:https://stackoverflow.com/questions/39333079/jstl-loop-counter-3-increment