freemarker报错:Tip: If the failing expression is known to be legally null/missing, either specify a de

本文介绍Freemarker模板引擎中如何正确处理空值和未定义变量的问题,避免运行时报错,并提供了一种推荐的写法。

freemarker报错:Tip: If the failing expression is known to be legally null/missing, either specify a default value with 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 parenthessis: (myOptionVar.foo)!myDefault, (myOptionVar.foo)??



大致意思是所写的表达式应判定不为空或者指定一个默认值,貌似ftl的<#if>判断一般都一定要判断一下不能为空

错误写法(后台会报错,实际上可以允许):

</pre><p><pre name="code" class="html"><span style="font-size:18px;"><#if lists?? && lists?size gt 0>
<#list lists as list>
<option value="${list.id}" <#if outbean.id == '${list.id}'> selected="selected" </#if> >${list.name!''}</option>
</#list>
</#if></span>

纠正写法:

<#if lists?? && lists?size gt 0>
<#list lists as list>
<option value="${list.id}" <#if (outbean.id!'') == '${list.id}'> selected="selected" </#if> >${list.name!''}</option>
</#list>
</#if>


FreeMarker template error: The following has evaluated to null or missing: ==> this.escapeHtml [in template "manageparty/managePages/planSatrt.html" at line 915, column 40] Tip: If the failing expression is known to be legally null/missing, either specify a default value with 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 parenthessis: (myOptionVar.foo)!myDefault, (myOptionVar.foo)?? The failing instruction (FTL stack trace): ---------- ==> ${this.escapeHtml(risk.id)} [in template "manageparty/managePages/planSatrt.html" at line 915, column 38] ---------- Java stack trace (for programmers): ---------- freemarker.core.InvalidReferenceException: [... Exception message was already printed; see it above ...] at freemarker.core.InvalidReferenceException.getInstance(InvalidReferenceException.java:98) at freemarker.core.UnexpectedTypeException.newDesciptionBuilder(UnexpectedTypeException.java:41) at freemarker.core.UnexpectedTypeException.<init>(UnexpectedTypeException.java:24) at freemarker.core.MethodCall._eval(MethodCall.java:119) at freemarker.core.Expression.eval(Expression.java:111) at freemarker.core.Expression.evalAndCoerceToString(Expression.java:115) at freemarker.core.DollarVariable.accept(DollarVariable.java:76) at freemarker.core.Environment.visit(Environment.java:265) at freemarker.core.MixedContent.accept(MixedContent.java:93) at freemarker.core.Environment.visit(Environment.java:265) at freemarker.core.Environment.process(Environment.java:243) at freemarker.template.Template.process(Template.java:277) at org.sevenstar.web.view.freemarker.FreemarkerResult.flow(FreemarkerResult.java:182) at org.sevenstar.web.ApplicationDispatcher.execute(ApplicationDispatcher.java:109) at org.sevenstar.web.ApplicationFilter.doFilter(ApplicationFilter.java:89) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) 还是 activeRisks.forEach((risk, index) => { html += ` <tr data-id="${this.escapeHtml(risk.id)}"> <td>${index + 1}</td> <td>${this.escapeHtml(risk.power_plant_unit)}</td> <td>${this.escapeHtml(risk.wot)}</td> <td>${this.escapeHtml(risk.zymc)}</td> <td>${this.escapeHtml(risk.dept)}</td> <td>${this.escapeHtml(risk.gzr)}</td> <td>${this.escapeHtml(risk.start_date )}</td> <td>${this.escapeHtml(risk.end_date)}</td> <td>${this.escapeHtml(risk.zyfx)}</td> <td>${this.escapeHtml(risk.fxgkcs)}</td> <td>${this.escapeHtml(risk.jyfk_tzyj)}</td> <td class="actions"> <button class="btn btn-sm edit-risk-btn" data-id="${this.escapeHtml(risk.id)}"> <i class="fas fa-edit"></i> 编辑 </button> <button class="btn btn-sm btn-danger delete-risk-btn" data-id="${this.escapeHtml(risk.id)}"> <i class="fas fa-trash"></i> 删除 </button> </td> </tr> `; });这个地方报错
最新发布
10-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值