Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 速度空值和空字符串_Java_Templates_Velocity - Fatal编程技术网

Java 速度空值和空字符串

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'

在速度中,我有一个变量,它的值为空。我不想在那种情况下展示任何东西

目前模板引擎将“”转换为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' 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页面很有趣:。也请查看