【问题标题】:Velocity nulls and empty strings速度 null 和空字符串
【发布时间】:2012-09-04 12:30:09
【问题描述】:

在速度方面,我有一个变量,其值为 null。在这种情况下,我不想显示任何内容。

目前模板引擎将 "" 翻译成 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

【问题讨论】:

标签: java templates velocity


【解决方案1】:

$!a 可以解决问题。您可以直接使用此表单,无需 if 检查。

【讨论】:

    【解决方案2】:

    你想要安静的参考符号:$!a

    这是你的例子:

    This is the variable $!a.
    

    如果$anull"",Velocity 将呈现:

    This is the variable .
    

    官方指南部分: https://velocity.apache.org/engine/devel/user-guide.html#quietreferencenotation

    【讨论】:

      【解决方案3】:

      另一种选择是根据 Checking for Null 修改您的 if 语句(感谢@xavi-lópez 的链接):

      方法 2:使用 null 被评估为空字符串的事实 安静的参考。 (参见 http://velocity.apache.org/engine/devel/user-guide.html#quietreferencenotation)

      所以,你的代码是:

      #set ( $a = "")
      #if ("$a" != "") 
         assert("never prints a neither gets here: " + $a)
      #end
      

      【讨论】:

        猜你喜欢
        • 2011-11-25
        • 1970-01-01
        • 2017-03-31
        • 2013-12-01
        • 1970-01-01
        • 2014-09-22
        • 1970-01-01
        • 2014-04-19
        • 1970-01-01
        相关资源
        最近更新 更多