velocity2.0 中的 $velocityCount 不起作用

the $velocityCount in velocity2.0 not work

当我将速度从 1.7 更新到 2.0 时,$velocityCount 不起作用,并出现错误:

[org.apache.velocity.rendering] - Left side ($velocityCount) of comparison operation has null value 

发生什么事了?

我的代码如下:

"images": [
       #foreach($image in $cl.images)
        #if($velocityCount <= 3)
        #set($img = $cot.encryptUrlForTrans($image.imageUrl))
        #set($_img = "http://tf.example.com/tc/img/")
        #if($velocityCount != 1),#end
        {
           "url": "$_img$img?enable=&w=120&h=80&cut=",
           "width": 120,
           "height": 80
         }
     #end
     #end
     ]

#set($img = $cot.encryptUrlForTrans($image.imageUrl)) 行有 NullPointerException 吗?如果可以执行 $image 必须不为空,我不知道错误是怎么发生的?因为方法 encryptUrlForTrans(null) 具有正确的 return 值。

您需要遵循 upgrading instructions 并检查日志以了解之前的错误。

需要更改依赖项并且

To maximize backward compatibility with Velocity 1.x, be sure to include the following lines in your Velocity configuration:

runtime.conversion.handler = none space.gobbling = bc

只需对从零开始的索引使用 $foreach.index,或者对从 1 开始的计数器使用 $foreach.count

当然,按照user7294900的建议检查upgrading instructions

我正在使用 2.0,$velocityCount 不起作用,但是 $foreach.index 和 $foreach.count 都可以。