JSTL LOOP - counter +=3 Increment

半城伤御伤魂 提交于 2020-01-03 06:41:10

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!