Velocity 对于数组的操作功能不是很好,比如说,要输出数组的第 3 个元素,除了 foreach 外没有其他办法了。如果 foreach 中是集合的话,Velocity 可以直接调用方法的,调用集合的 size() 可以获得它的大小。然后再用 $velocityCount 来判断元素的,$velocityCount 是 foreach 内置的值,默认从 1 开始,再用 size() 就可以看出哪个是第一个,哪个是第二个了。#set($size = $list.size()) #foreach($obj in $list) #if($velocityCount == 1) this is first value #end #if($velocityCount == $size) this is last value #end $obj.name #end
就可以看出哪个是第一个,哪个是第二个了。#set($size = $list.size())
#foreach($obj in $list)
#if($velocityCount == 1)
this is first value
#end
#if($velocityCount == $size)
this is last value
#end
$obj.name
#end