Java 速度空值和空字符串
在速度中,我有一个变量,它的值为空。我不想在那种情况下展示任何东西 目前模板引擎将“”转换为null,因此我必须这样做Java 速度空值和空字符串,java,templates,velocity,Java,Templates,Velocity,在速度中,我有一个变量,它的值为空。我不想在那种情况下展示任何东西 目前模板引擎将“”转换为null,因此我必须这样做 #set ( $a = "") #if ($a) assert("never prints a neither gets here: " + $a) #end 有没有一种方法可以让我直接这么做?我希望能够制作如下内容: This is the variable $a. ## in case that $a is null i don't want 'dollar a'
#set ( $a = "")
#if ($a)
assert("never prints a neither gets here: " + $a)
#end
有没有一种方法可以让我直接这么做?我希望能够制作如下内容:
This is the variable $a. ## in case that $a is null i don't want 'dollar a' to be displayed
$!一个
就行了。如果勾选,您可以不使用直接使用此表单。您需要安静的参考符号:$!a
以下是您的示例:
This is the variable $!a.
如果$a
为null
或”
,则Velocity将呈现:
This is the variable .
官方指南部分:
另一种选择是根据修改您的if
语句(感谢链接@xavi-lópez):
方法2:使用null作为空字符串计算的事实
安静的参考资料。(参见。
)
因此,您的代码是:
#set ( $a = "")
#if ("$a" != "")
assert("never prints a neither gets here: " + $a)
#end
您可能会发现以下Velocity wiki页面很有趣:。也请查看