获取方式
- 使用${velocityCount} 来获取
- 使用${foreach.index} 来获取(推荐)
- 最近在网上看到很多文档说的获取索引下标通过${velocityCount} 来获取,但是这个在velocity.properties中配置的初始值是从1开始的,我们需要在初始化vm引擎后,对其初始值进行配置,配置如下:
// 设置 $velocityCount 初始值为0
// 已过时,采用$foreach.index 来代表索引下标 $foreach.count 来代表长度
VelocityEngine velocityEngine = new VelocityEngine();
velocityEngine.setProperty("directive.foreach.counter.initial.value", 0);
(不建议使用此方式,因为在Velocity 2.0后,${velocityCount} 这种方式将被移除)
- 使用${foreach.index} 来获取, 这也是官方推荐使用的方式,${foreach.index}默认就是从0开始, 如果你想获取当前计数,默认从1开始,就像${velocityCount}一样,可以通过${foreach.count}来获取。