Thymeleaf:使用#session时偶尔发生的错误-评估SpringEL表达式的异常

时间:2019-03-02 18:10:12

标签: thymeleaf cas

我们正在使用Spring webflow + ThymeLeaf并尝试在html页面中访问session.getAttribute()。

Thymeleaf有点陌生,我了解Thymeleaf有2种方法可以解决会话问题。 $ {session.something}和$ {#session.getAttribute('something')}。

我们正在使用的代码如下所示,有时会失败。

<div th:if="${(#session.getAttribute('booleanAttribute'))}">  
...
</div>

在本地环境中,我从未看到过失败,并且它按预期运行。在生产中,这失败了。 30分钟内200次出现以下错误-

org.thymeleaf.exceptions.TemplateProcessingException:  Exception evaluating SpringEL expression: "(#session.getAttribute('booleanAttribute'))" (template: "base" - line 80, col 10)

在不了解为什么在本地工作正常的情况下,我几乎不愿意进行null检查以查看(#session)是否为null。所以我有这个问题-

上面可能有什么问题,如何在本地复制,以便确认所放置的修复程序将在所有环境中正常工作?

1 个答案:

答案 0 :(得分:0)

根据docs

  

#session:直接访问与当前请求关联的javax.servlet.http.HttpSession对象。

在我的测试中,会话期满时#session为空。如果用户的会话已过期,则使用#session会引发空点异常(Method call: Attempted to call method getAttribute(java.lang.String) on null context object)。您应该可以通过删除JSESSIONID cookie来进行测试。

另一方面,

${session}SessionAttributesMap,即使没有有效的会话,它也永远不会为空。在这种情况下,表达式${session.booleanAttribute}仍然可以工作,只是求值为false。