Thymeleaf:使用#session偶尔出错 - 评估SpringEL表达式的异常

问题描述 投票:1回答:1

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

对Thymeleaf来说是新手,我知道Thymeleaf有2种方法可以解决会议问题。 $ {session.something}和$ {#session.getAttribute('something')}。

我们使用的代码就像下面的代码偶尔会失败。

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

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

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

我不太愿意放置空检查以查看(#session)是否为null而不理解为什么在本地工作正常。所以我有这个问题 -

上面可能有什么问题,我如何在本地重现,以便我可以确认我要放置的修复程序将适用于所有环境?

thymeleaf cas
1个回答
0
投票

根据docs

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

在我的测试中,当会话到期时,#session为null。如果用户的会话已过期,则使用#session会抛出零点异常(Method call: Attempted to call method getAttribute(java.lang.String) on null context object)。你应该能够通过删除你的JSESSIONID cookie来测试这个。

另一方面,${session}是一个SessionAttributesMap,它似乎永远不会为空 - 即使没有有效的会话。在这种情况下,表达式${session.booleanAttribute}仍然可以工作,只评估为false。

© www.soinside.com 2019 - 2024. All rights reserved.