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 $ ...
随机推荐
- idea设置utf-8
- 微信小程序开发之搞懂flex布局2——flex container
容器的概念,是用来包含其它容器(container)和项目(item). flex container——flex容器 A flexbox layout is defined using the fl ...
- 02.02.03第3章 餐饮项目案例(Power BI商业智能分析)
02.02.03第3章 餐饮项目案例 02.02.03.01餐饮数据理解与读入 00:06:12 02.02.03.02餐饮数据处理 00:29:57 处理生成的表为: 02.02.03.03餐饮数据 ...
- 02.02.02 第2章 制作power bi图表(Power BI商业智能分析)
---恢复内容开始--- 02.02.02第2章 制作power bi图表 02.02.02.01 power pivot数据导入 00:08:43 02.02.02.02建立数据透视表 00:11: ...
- C++枚举类型Enum及C++11强枚举类型用法
C++中的枚举类型常常和switch配合使用,这里用一个简单的switch控制键盘回调的代码片段来说明枚举的用法: //W A S D 前.后.左.右行走 enum Keydown{ Forward= ...
- django中使用memcache的一些注意事项
最近写django项目时在保存验证码方面要用到memcached,于是便查看了一些教程进行操作,结果确遇到了一系列问题,以下是一些容易遇到的雷区: 1.windows下memcached安装: -wi ...
- node平台的安装与搭建
1.node.js 官网:https://nodejs.org/ (.org:是非盈利机构,他们的软件是不收费的,但是服务收费) 安装完以后的检测指令:node -v (在这里安装需要注意一 ...
- FPGA开发随笔汇总
点击标题即可进入相关随笔. DE-SOC开发板VrilogHDL开发相关部分: (本过程需要Verilog HDL 的基本语言基础) 1.FPGA的发展史及FPGA 的基础架构 2.首先看一下友晶DE ...
- DataOutputStream and DataInputStream
1.在io包中,提供了两个与平台无关的数据操作流 数据输出流(DataOutputStream) 数据输入流(DataInputStream) 2.通常数据输出流会按照一定的格式将数据输出,再通过数据 ...
- 河北大学python选修课00次作业
学习python认为挺好玩的一件事.看到很多关于python的东西在网上,看到有这个课,认为只是选修课,别人也可以选,自己想不能被别人落下,别人都会,我不会可不行. 而且认为python是一个很强大的 ...