Velocity CheckingForNull
Q: I want to check for null, something like this:
#if ($car.fuel == null)
A: There are several approaches. Select the one most suitable depending on what you really want to do.
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.
Approach 4: Use a Tool that can check for null (NullTool,ViewNullTool).
#if( $null.isNull($car.fuel) )
Note: Of course, NullTool must be in the Context as $null in this case.
Approach 5: Don't check for null directly, use a self-explaining method.
#if( $car.fuelEmpty )
Note: This is my (Shinobu Kawai's) recommended solution. You have to implement the method, but it makes the template so easy-to-read.
public boolean isFuelEmpty()
{
// return true if fuel is empty.
}
Approach 6: Use a custom directive. cf. IfNullDirective, IfNotNullDirective
#ifnull( $car.fuel )
#ifnotnull( $car.fuel )
Note: You will have to register the directive in your velocity.properties.
userdirective = org.apache.velocity.tools.generic.directive.Ifnull
userdirective = org.apache.velocity.tools.generic.directive.Ifnotnull
Velocity CheckingForNull的更多相关文章
- velocity 判断 变量 是否不是空或empty
原先的 #if($mobile) 这种写法是不准确的 ,请换成 "$!{ mobile}"!="" 说明 : #if($mobile) 这种写法 只能 ...
- Velocity笔记--使用Velocity获取动态Web项目名的问题
以前使用jsp开发的时候,可以通过request很轻松的获取到根项目名,现在换到使用velocity渲染视图,因为已经不依赖servlet,request等一些类的环境,而Web项目的根项目名又不是写 ...
- Velocity初探小结--velocity使用语法详解
做java开发的朋友一般对JSP是比较熟悉的,大部分人第一次学习开发View层都是使用JSP来进行页面渲染的,我们都知道JSP是可以嵌入java代码的,在远古时代,java程序员甚至在一个jsp页面上 ...
- Velocity初探小结--Velocity在spring中的配置和使用
最近正在做的项目前端使用了Velocity进行View层的数据渲染,之前没有接触过,草草过了一遍,就上手开始写,现在又回头细致的看了一遍,做个笔记. velocity是一种基于java的模板引擎技术, ...
- 记录一次bug解决过程:velocity中获取url中的参数
一.总结 在Webx的Velocity中获取url中参数:$rundata.getRequest().getParameter('userId') 在Webx项目中,防止CSRF攻击(Cross-si ...
- velocity中$springMacroRequestContext.getMessage($code)
在Java国际化(i18n)中, vm页面显示内容需要使用 #springMessage("title") 实际运行时发现页面输出$springMacroRequestContex ...
- Velocity 局部定制模板
Velocity介绍 Velocity是一个基于java的template engine.它允许Web designer引用Java Code中定义的方法.Web designer可以和Java工程师 ...
- 只需2分钟,简单构建velocity web项目
Velocity是一个基于java的模板引擎(template engine).它允许任何人仅仅简单的使用模板语言(template language)来引用由java代码定义的对象 velocity ...
- Velocity简单语法及VelocityHelper封装
1.简单替换##这是注释Wellcome ${userName}! Now:$date 2.申明变量:#set( $iAmVariable = "good!" )Welcome $ ...
随机推荐
- godoc
Godoc-一个Go代码文档化工具 Python - Docstring Java - javadoc
- BSOJ3760||洛谷P1453 城市环路 题解
城市环路 Description 一座城市,往往会被人们划分为几个区域,例如住宅区.商业区.工业区等等.B市就被分为了以下的两个区域——城市中心和城市郊区.在着这两个区域的中间是一条围绕B市的环路,环 ...
- RNQOJ 21 FBI数
如果字符串全是0输出B,全是1输出I,01混合输出F,如果字符串分解到只剩下一个字符的时候我们可以很简单的判断出来是B串还是I串,如果处在父节点的位置,这里运用递归,通过子节点的返回值来判断子节点是混 ...
- Apache万网SSl证书安装不成功的一个注意事项(https安全链接,基于phpstudy)
最近,微信小程序挺火,要做小程序网站域名必须要有ssl证书,形成https://的安全链接,我于是从万网上下载了一个免费的ssl证书,按照万网提供的安装方式怎么也安装不好,最后我这个Apache小白费 ...
- Java面试题之Redis
1.redis数据结构有哪些? string,list,hash,set,zset 2.redis为什么是单线程的? redis是基于内存的操作,cpu不是redis的瓶颈,内存大小或网络带宽才是: ...
- 解决:Adobe Acrobat Pro中设置背景颜色后,出现白色条纹
找到 编辑->首选项->页面显示->渲染->使用2D图形加速 取消即可
- drools规则引擎中易混淆语法分析_相互触发导致死循环分析
整理了下最近在项目中使用drools出现的问题,幸好都在开发与测试阶段解决了,未波及到prod. 首先看这样两条规则: /** * 规则1_set默认利率a */ rule "rate_de ...
- Codeforces Round #485 (Div. 2) D. Fair
Codeforces Round #485 (Div. 2) D. Fair 题目连接: http://codeforces.com/contest/987/problem/D Description ...
- (转)pycharm常用快捷键
Alt+Enter 自动添加包Ctrl+t SVN更新Ctrl+k SVN提交Ctrl + / 注释(取消注释)选择的行Ctrl+Shift+F 高级查找Ctrl+Enter 补全Shift + En ...
- js基础知识1:标识符 注释
何为标识符:意思是标记识别的的符号,简称标识符,主要的用途声明变量,函数的名字,属性以及函数中的参数. 标识符的规则1:首先第一个字符必须是字母(abcABC),_(下划线),$(美元符号).不能是( ...