gpt4 book ai didi

velocity - 速度,检查字符串是否为空而不为空的最有效方法是什么

转载 作者:行者123 更新时间:2023-12-03 09:35:16 27 4
gpt4 key购买 nike

我经常遇到字符串值不存在和/或为空的情况。这是测试这种情况的最佳方法吗?

#if( $incentive.disclaimer && $!incentive.disclaimer != '' ) 
$incentive.disclaimer
#end

最佳答案

如果您只想让Velocity在该处显示该值,或者在不存在时不显示任何值,那么quiet reference本身就能解决问题:

$!incentive.disclaimer

如果要显式测试是否为空,则 StringUtils中的 Apache Commons Lang可以提供帮助。首先将其添加到您的上下文(引用 here):
context.put("StringUtils", StringUtils.class);

尽管如果您使用的是旧版本的Velocity,则可能不喜欢类引用,因此可以添加一个实例:
context.put("StringUtils", new StringUtils());

然后,您可以从Velocity模板中调用其 isEmpty方法:
#if($StringUtils.isEmpty($incentive.disclaimer))
## logic here...
#end

如果要将空白视为空,则还有 isBlank

关于velocity - 速度,检查字符串是否为空而不为空的最有效方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13158441/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com