Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to print decremental numbers like:

<c:forEach var="i" begin="10" end="0" step="-1">
    ... ${i} ...
</c:forEach>

then I got jsp exception:

javax.servlet.jsp.JspTagException: 'step' <= 0
    javax.servlet.jsp.jstl.core.LoopTagSupport.validateStep(LoopTagSupport.java:459)
    org.apache.taglibs.standard.tag.rt.core.ForEachTag.setStep(ForEachTag.java:60)
    ....

but this answer says it is possible to loop in both ways:

JSTL forEach reverse order

What's wrong with me?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
353 views
Welcome To Ask or Share your Answers For Others

1 Answer

I am not sure how the answerer of the other question got it to work, but I can't get it to work here with the reference JSTL implementation.

Anyway, you can achieve the requirement with following:

<c:forEach var="i" begin="0" end="10" step="1">
    ... ${10 - i} ...
</c:forEach>

Or if you'd like to avoid duplication of 10:

<c:forEach var="i" begin="0" end="10" step="1" varStatus="loop">
    ... ${loop.end - i + loop.begin} ...
</c:forEach>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...