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 $ ...
随机推荐
- Netty使用(一)
什么是Netty Netty由JBOSS提供的基于Java NIO的开源框架,Netty提供异步非阻塞.事件驱动.高性能.高可靠.高可定制性的网络应用程序和工具, 可用于开发服务端和客户端. 配置服务 ...
- nginx简介与配置
nginx简介 nginx(发音同engine x)是一款轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like协议下发行. nginx由俄罗斯的程序 ...
- Java关键字this与super
this有两个用途: 1.引用隐式参数(出现在方法名前的参数,显示参数是出现在方法名后位于括号里的参数,如:e.raiseSalary(10%),e是隐式参数,10%是显示参数):这里比较常见.形式如 ...
- sjms-3 结构型模式
结构型模式 适配器模式 内容:将一个类的接口转换成客户希望的另一个接口.适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作.两种实现方式:类适配器:使用多继承对象适配器:使用组合 角色 ...
- JAVA 8 主要新特性 ----------------(五)Lambda方法引用与构造器引用
一.Lambda方法引用 当要传递给Lambda体的操作,已经有实现的方法了,可以使用方法引用!(实现抽象方法的参数列表,必须与方法引用方法的参数列表保持一致!) 方法引用: 使用操作符 “::” 将 ...
- spring 3.1.1 mvc HanderMapping源码
https://my.oschina.net/zhangxufeng/blog/2177464 RequestMappingHandlerMapping getMappingForMethod /** ...
- 快乐python 零基础也能P图 —— PIL库
Python PIL PIL (Python Image Library) 库是Python 语言的一个第三方库,PIL库支持图像存储.显示和处理,能够处理几乎所有格式的图片. 一.PIL库简介 1. ...
- C# WebAPI系列(2)
上篇中简单介绍了一下WebApi,本章主要介绍一下Controller相关的知识. 在实际应用中,Controller是WebAPI的链接服务器和客户端的窗口.Controller的好坏影响整个系统的 ...
- shell丢弃信息
1>/dev/null 2>&1 或 >/dev/null 2>&1 的含义及注意事项 > 表示重定向到哪 1表示stdout标准输出,输出到屏幕 ...
- SVN设置全局忽略提交文件或者目录
在目录中右击选择TortoiseSVN -> 设置 -> 常规设置 -> 全局忽略样式 修改全局忽略样式(可以设置忽略的文件或者目录,以空格隔开):*.o *.lo *.la *.a ...