Velocity 将 int i = 0 解释为 boolean false

问题描述

如果我检查 var int i = 0 是否存在,我会得到错误的结果:

#if($i)
  <output>$i</output>
#end

结果:

i not defined --> no output
i = 0         --> no output
i = 1         --> <output>1</output>

如何告诉速度考虑定义的值 0?

i not defined --> no output
i = 0         --> <output>0</output>
i = 1         --> <output>1</output>

解决方法

您可以使用 operator

#if($i >= 0)
  <output>$i</output>
#end