hel*_*004 3 java velocity velocity-tools
我试图在 Velocity 中将变量设置为 null。我在尝试:
#set ($acessSpeed = null)
Run Code Online (Sandbox Code Playgroud)
我正在阅读速度空支持维基。它说我们可以通过这种方式将值设置为 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
Run Code Online (Sandbox Code Playgroud)
我可以为每个 if 块使用不同的变量,但我想知道是否有更简单的方法来做到这一点。
任何的意见都将会有帮助。
这取决于您的配置。要执行您需要的操作,您需要使用以下方式配置 Velocity:
directive.set.null.allowed = true
Run Code Online (Sandbox Code Playgroud)
然后,您可以使用以下命令将变量设置为 null:
#set($foo = $null)
Run Code Online (Sandbox Code Playgroud)
其中 $null 只是一个未定义的变量。
否则,如果唯一目的是测试变量,那么一个方便的技巧是将其值设置为 false。
#set($foo = false)
#if($foo) this will be displayed #end
Run Code Online (Sandbox Code Playgroud)