---
title: "FreeMarker 对null值的处理"
author: "ych0108"
date: "2014-05-05T17:40:44Z"
tags: ["HTML", "Java", "freemarker"]
categories: ["Web前端"]
slug: "freemarker-null"
draft: false
lastmod: "2014-05-05T17:40:44Z"
id: 2061750
url: "https://ych0108.iteye.com/blog/2061750"
---

以下引用官方描述： \[code="java"]The FreeMarker template language doesn't know the Java language null at all. It doesn't have null keyword, and it can't test if something is null or not.\[/code] 1.判断是否存在,通过exists关键字或者"??"运算符。都将返回一个布尔值 \[code="html"]user.name?exists user.name?? Html代码 收藏代码 &lt;#if user.name?exists&gt; //TO DO &lt;#if user.age??&gt; //TO DO \[/code] 2.忽略null值 \[code="html"]假设前提：user.name为null ${user.name}，异常 ${user.name!},显示空白 ${user.name!'vakin'}，若user.name不为空则显示本身的值，否则显示vakin ${user.name?default('vakin')}，同上 ${user.name???string(user.name,'vakin')},同上\[/code] eg:当outCar.startNumberKm为空时则取startNumberKmByLast作用域中的值 \[code="html"]

起始公里数(km)：

*\**

\[/code]