velocity:除了在最后一次循环迭代中做一些事情

在速度方面,我想在最后一个循环中做一些不同的事情。

什么是正确的习语?

相关 : java中增强for循环的最后一次迭代

如果您在最后一次迭代中,可以使用测试::

#foreach( $item in $list ) $item.text #if( $foreach.hasNext ), #end #end 

@schechecheck的答案是你需要的,但请注意$foreach变量仅在速度1.7中可用,如果你使用的是早期版本,你可以使用:

 #foreach( $item in $list ) $item.text #if( $velocityHasNext ), #end #end 

但是, $velocityHasNext变量在版本1.7中已弃用,在2.0中已删除,而使用$foreach.hasNext

我使用的习惯用法是保存在循环未完成时要添加的可选文本。

 #set($sep = "") #foreach($item in $list) $sep$item #set($sep = ", ") #end 

这在Velocity的旧版本中对我有用

 #if($velocityCount < $list.size()), #end