无法在速度模板

时间:2017-04-08 18:56:21

标签: java velocity velocity-tools

我试图在Velocity中将变量设置为null。我在尝试:

#set ($acessSpeed = null)

我正在阅读velocity null支持wiki。它说我们可以通过这种方式将值设置为null。 https://wiki.apache.org/velocity/VelocityNullSupport

但是当我尝试它时,我得到一个错误说"遇到" null"在...."

我遇到的问题我有一个包含多个if块的巨大模板,如果条件满足则会执行。因此,在每个if块的末尾,我需要将accessSpeed的值设置为null。

#if (some condition)
     access speed value set here.
.
.
.
#end
// I need to set the access speed value to null here.
#if (some other condition)
    access speed value to be set to something again.
.
.
.
#end

我可以为每个if块使用不同的变量,但我想知道是否有更简单的方法。

任何建议都会有所帮助。

1 个答案:

答案 0 :(得分:2)

It depends upon your configuration. To do what you need, you need to configure Velocity with:

directive.set.null.allowed = true

Then, you can set your variable to null with:

#set($foo = $null)

where $null is just an undefined variable.

Otherwise, if the only purpose is to test the variable, then a convenient trick is to set its value to false.

#set($foo = false)
#if($foo) this will be displayed #end