FreeMarker空值异常:

    技术2022-07-10  125

    freemarker 报错: username:'FreeMarker template error (DEBUG mode; use RETHROW in production!): The following has evaluated to null or missing: ==> profile [in template “inc/layout.ftl” at line 28, column 21] Tip: If the failing expression is known to 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)??

    大致意思是某个对象为null或者缺失,导致表达式不合法,可以指定默认值,指定方式是:myOptionalVar!myDefault 或者<#if myOptionalVar??>when-present<#else>when-missing</#if>. (这仅仅覆盖最后一个表达式,如果要覆盖整个表达式使用:(myOptionalVar.foo)!myDefault, (myOptionalVar.foo)??) 我的错误在:

    <script> layui.cache.user = { username:'${profile.username!"游客"}' ,uid:${profile.id!"-1"} ,avatar: '${profile.avatar!"/res/images/avatar/00.jpg"}' ,experience: 83 ,sex: '${profile.sex!"男"}' }; </script>

    提示==> profile [in template “inc/layout.ftl” at line 28, column 21]为空,于是修改为带括号的表达式解决问题:

    <script> layui.cache.user = { username:'${(profile.username)!"游客"}' ,uid:${(profile.id)!"-1"} ,avatar: '${(profile.avatar)!"/res/images/avatar/00.jpg"}' ,experience: 83 ,sex: '${(profile.sex)!"男"}' }; </script>

    不在报错,解决问题。

    Processed: 0.009, SQL: 9