JSTL c:forEach,递减数循环不可能? [英] JSTL c:forEach, decremental number loop impossible?

查看:26
本文介绍了JSTL c:forEach,递减数循环不可能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打印递减数字,例如:

I want to print decremental numbers like:

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

然后我得到了jsp异常:

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逆序

我怎么了?

推荐答案

我不确定另一个问题的回答者是如何让它工作的,但我无法使用参考 JSTL 实现让它在此处工作.

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>

或者,如果您想避免重复 10:

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>

这篇关于JSTL c:forEach,递减数循环不可能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆