freemaker为null和为空串""的判断

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34988304/article/details/86623396

freemaker中判断值是否为null:

<#if ${flag.a??}>
	flag.a不为null的处理
<#else>
	flag.a为null的处理
</#if>

freemaker中判断值是否为"":

<#if ${flag.a!""}>
	flag.a不为""的处理
<#else>
	flag.a为""的处理
</#if>

java代码:

Map<String, Object> flag = new HashMap<>();
Date a = null;
flag.put("a", new Date());
flag.put("b", a);
flag.put("c", "");

freemaker页面代码:

[#if flag.a??]
	时间不为空的值:${flag.a?string('yyyy-MM-dd')}
[#else]
	时间不为空的值:1
[/#if]
<br><br>
[#if flag.b??]
	时间为null的值:${flag.b?string('yyyy-MM-dd')}
[#else]
	时间为null的值:2
[/#if]
<br><br>
[#if flag.c?? && flag.c!""]
	时间为""的值:${flag.b?string('yyyy-MM-dd')}
[#else]
	时间为""的值:3
[/#if]
<br><br>
[#if flag.c??]
	时间为""的值:${flag.b?string('yyyy-MM-dd')}
[#else]
	时间为""的值:3
[/#if]

数据结果


时间不为空的值:2019-01-24 

时间为null的值:2 

时间为""的值:3 

时间为""的值:此处报错,报错信息:

严重: Servlet.service() for servlet [SpringMVC] in context with path [] threw exception [Request processing failed; nested exception is freemarker.core.InvalidReferenceException: The following has evaluated to null or missing:
==> flag.b [in template “bulkxl/classWorkOrder/add.ftl” at line 44, column 19]

Tip: It’s the step after the last dot that caused this error, not those before it.

Tip: If the failing expression is known to be legally refer to something that’s sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use [#if myOptionalVar??]when-present[#else]when-missing[/#if]. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)??

FTL stack trace ("~" means nesting-related):
- Failed at: ${flag.b?string(“yyyy-MM-dd”)} [in template “bulkxl/classWorkOrder/add.ftl” at line 44, column 17]
----] with root cause

猜你喜欢

转载自blog.csdn.net/qq_34988304/article/details/86623396