$null 检查速度

发布于 2024-09-14 09:44:16 字数 261 浏览 3 评论 0原文

我通过您更新的资源了解到速度 1.6 中使用 $null 进行 null 检查。 资源:读取 Velocity 模板中映射的模型对象 但我面临着如此多的挑战,以至于没有 $null 用于空检查速度,因为没有提供有关此的文档。 请向我提供文档,说明 $null 对于速度中的 null 检查有效。

I came to know about null check using $null in velocity 1.6 through a resource updated by you.
Resource: Reading model objects mapped in Velocity Templates
But I am facing so many challenges that there is no $null for null check in velocity as there is no documentation provided about this.
Please provide me with documentation stating $null as valid for null check in velocity.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

要走干脆点 2024-09-21 09:44:16

要检查变量是否不为空,只需使用 #if ($variable)

#if ($variable) 
 ... do stuff here if the variable is not null
#end

如果您需要在变量为空时执行操作,只需否定测试

#if (!$variable)
 ... do stuff here if the variable is null
#end

To check if a variable is not null simply use #if ($variable)

#if ($variable) 
 ... do stuff here if the variable is not null
#end

If you need to do stuff if the variable is null simply negate the test

#if (!$variable)
 ... do stuff here if the variable is null
#end
故笙诉离歌 2024-09-21 09:44:16

方法 1:利用 null 被评估为错误条件这一事实。 (参见 http://velocity.apache.org/engine/devel /user-guide.html#Conditionals)

#if( ! $car.fuel )

注意:如果 $car.fuel 的结果是
布尔值假。这种方法实际上检查的是是否
引用为空或错误。

方法 2:利用 null 在安静引用中被计算为空字符串的事实。 (参见 http://velocity.apache.org/engine/devel /user-guide.html#quietreferencenotation)

#if( "$!car.fuel" == "" )

注意:如果 $car.fuel 的结果是
空字符串。这种方法实际上检查的是是否
引用为空或为空。顺便说一句,只需检查是否为空即可
实现方式:

#if( "$car.fuel" == "" )

方法 3:结合方法 1 和 2。这将检查 null 和仅 null。

#if ((! $car.fuel) && ("$!car.fuel" == ""))

注意:这里的基本逻辑是:“(null 或 false)和(null 或

<块引用>

empty-string)" => 如果为 true,则必须为 null。这是 true,因为“false 和空字符串且非 null”永远不会为 true。恕我直言,这使得
模板太复杂,难以阅读。

Approach 1: Use the fact that null is evaluated as a false conditional. (cf. http://velocity.apache.org/engine/devel/user-guide.html#Conditionals)

#if( ! $car.fuel )

Note: The conditional will also pass if the result of $car.fuel is the
boolean false. What this approach is actually checking is whether the
reference is null or false.

Approach 2: Use the fact that null is evaluated as an empty string in quiet references. (cf. http://velocity.apache.org/engine/devel/user-guide.html#quietreferencenotation)

#if( "$!car.fuel" == "" )

Note: The conditional will also pass if the result of $car.fuel is an
empty String. What this approach is actually checking is whether the
reference is null or empty. BTW, just checking for empty can be
achieved by:

#if( "$car.fuel" == "" )

Approach 3: Combine Approach 1 and 2. This will check for null and null only.

#if ((! $car.fuel) && ("$!car.fuel" == ""))

Note: The logic underlying here is that: "(null or false) and (null or

empty-string)" => if true, must be null. This is true because "false and empty-string and not null" is never true. IMHO, this makes the
template too complicated to read.

椒妓 2024-09-21 09:44:16

对于具有布尔值的变量,这可能会起作用:

#if ($variable || $variable == "false")
  ... if the boolean $variable is not null
#else
  ... if the boolean $variable is null
#end

For a variable with a boolean value, this might do the trick:

#if ($variable || $variable == "false")
  ... if the boolean $variable is not null
#else
  ... if the boolean $variable is null
#end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文