freemarker的classic_compatible设置,解决${xx.xx}报空错误

前段时间接触freemaker时,本来后端写各接口运行正常,但加入了模板后,频繁报空指针问题,整了许久,最后还是请教了别人解决了这个问题,现在记录下来,方便以后碰到了可以查阅。

错误样例如下:

ERROR: freemarker.runtime - Error executing FreeMarker template
FreeMarker template error:
The following has evaluated to null or missing:
==> user  [in template "include/header.ftl" at line 4, column 14]
 
----
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: #if user  [in template "include/header.ftl" at line 4, column 9]
    - Reached through: #include "/include/header.ftl"  [in template "index.ftl" at line 6, column 1]
----
 
Java stack trace (for programmers):
----
freemarker.core.InvalidReferenceException: [... Exception message was already printed; see it above ...]
    at freemarker.core.InvalidReferenceException.getInstance(InvalidReferenceException.java:131)
    at freemarker.core.UnexpectedTypeException.newDesciptionBuilder(UnexpectedTypeException.java:77)

可以看到,在读取user的时候,因为为空,报错了,错误处的代码是这样的

<#if user>

其实准确的写法应该是

<#if user??>

如果要消除错误,需要把前端代码修后成后面这种形式,这让一个后端开发的人来说很不合理啊,而且,模板中不止一处出现了这种写法。

请教以后把freemarker的配置修改如下:

一、若是springMVC框架则在spring-mvc-context.xml加一行下面代码,具体以你的springMVC配置文件取名为准:

 <!--解决空异常-->
 <prop key="classic_compatible">true</prop>

完整的如下:

 <!-- Freemarker配置 -->
    <bean id="freemarkerConfig" class="com.jagregory.shiro.freemarker.ShiroTagFreeMarkerConfigurer">
        <property name="templateLoaderPath" value="/WEB-INF/view/"/>
        <property name="freemarkerSettings">
            <props>
                <prop key="template_update_delay">0</prop>
                <prop key="default_encoding">UTF-8</prop>
                <prop key="boolean_format">true,false</prop>
                <prop key="locale">zh_CN</prop>
                <prop key="number_format">0.##########</prop>
                <prop key="date_format">yyyy-MM-dd</prop>
                <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
                <!--解决空异常-->
                <prop key="classic_compatible">true</prop>
                <prop key="template_exception_handler">ignore</prop>
                <!--用于支持key为non-string类型的map操作Map.get(Key)-->
                <prop key="object_wrapper">freemarker.ext.beans.BeansWrapper</prop>
                <prop key="auto_import">common/common.ftl as s</prop>
            </props>
        </property>
  </bean>

二、若是springBoot框架,则在application.properties中加入,具体以你的springboot的application配置文件取名为准,有的是application.yml的,自己参考格式修改即可:

#解决前台使用${}赋值值为空的情况
spring.freemarker.settings.classic_compatible = true

完整配置如下:

#视图模型
#req访问request
spring.freemarker.request-context-attribute=req
#后缀名
spring.freemarker.suffix=.ftl
spring.freemarker.content-type=text/html
spring.freemarker.enabled=true
#缓存配置
spring.freemarker.cache=false
#模板加载路径 按需配置
spring.freemarker.template-loader-path=classpath:/templates/
#编码格式
spring.freemarker.charset=UTF-8
#数字格式化,无小数点
spring.freemarker.settings.number_format='0.##'
#解决前台使用${}赋值值为空的情况
spring.freemarker.settings.classic_compatible = true
# 静态资源
spring.mvc.static-path-pattern=/static/**

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值