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 $ ...
随机推荐
- python3中 tkinter模块创建window窗体、添加按钮、事务处理、创建菜单等的使用
开始接触桌面图形界面编程,你可以到安装路径 \lib\tkinter 打开__init__.py 文件了解tkinter 1 tkinter 模块创建窗体,代码如下截图: 运行结果,如有右图显 ...
- 五分钟让你读懂UML常见类图
相信各位同学在阅读一些源码分析类文章或是设计应用架构时没少与UML类图打交道.实际上,UML类图中最常用到的元素五分钟就能掌握,经常看到UML类图但还不太熟悉的小伙伴赶紧来一起认识一下它吧:) 一 ...
- JSP页面使用include指令出现 Duplicate local variable basePath
现有三个页面 " include.jsp " " a.jsp " " b.jsp " 页面代码如下 首先是a.jsp <%@ page ...
- 移动端各种滚动场景需求的插件better-scroll
移动端各种滚动场景需求的插件: 文档地址: better-scroll:https://ustbhuangyi.github.io/better-scroll/doc/zh-hans/#better- ...
- 实战操作——通过wireshark查看任意qq好友IP
今天有群里好友问我如何查看别人IP,查IP这个东西对于一些刚入门的小白来说可能是一个比较高大上的玩意,但是实际上只需一个网络协议分析工具就OK,废话不多说,下面开始实践 实验对象:任意QQ好友 操作工 ...
- [置顶]ABP框架系列总目录(持续更新)
Articles & Tutorials Introduction & Step by Step With ASP.NET Core & Entity Framework Co ...
- 2019年华南理工校赛(春季赛)--I--炒股(简单思维水题)
水题,想想就过了 题目如下: 链接:https://ac.nowcoder.com/acm/contest/625/I来源:牛客网 攒机一时爽,一直攒机一直爽. 沉迷攒机的胡老师很快就发现,他每天只能 ...
- self_vs_default_definee_vs_receiver
最近在学习ruby的过程遇到很多有趣的博客,随记录学习,这篇学习笔记摘自http://yugui.jp/articles/846 #self ruby中self无处不在,或是显示的调用或是隐含调用,方 ...
- 代码文档生成工具Doxygen的使用备忘
Doxygen备忘 下载与安装 一般步骤 生成配置文件Doxyfile: doxygen (-s) -g 建立目录结构, 比如Doxyfile文件\doc文件夹(输出)\src文件夹(放代码) -&g ...
- Beta冲刺 (2/7)
Part.1 开篇 队名:彳艮彳亍团队 组长博客:戳我进入 作业博客:班级博客本次作业的链接 Part.2 成员汇报 组员1(组长)柯奇豪 过去两天完成了哪些任务 熟悉并编写小程序的自定义控件 展示G ...