我们正在使用Spring WebFlow Thymeleaf,并尝试访问HTML页面中的Session.getAttribute()。
对胸腺的新手,我知道百里叶有两种解决方案的方法。$ {session.something}和$ {#session.getAttribute('sosings')}。
我们正在使用的代码在下面类似于偶尔失败。
<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)
我几乎不愿意放置零检查以查看(#session)是否为null,而不了解为什么在本地工作。所以我有这个问题 -
上面有什么问题,我该如何在本地复制以确认我要放置的修复程序将在所有环境中工作?
根据文档:
#session
:直接访问javax.servlet.http.httpsession对象与当前请求关联的对象。
在我的测试中,#session
在会话到期时为无效。如果用户的会话过期,则使用#session
会引发NULL点异常(Method call: Attempted to call method getAttribute(java.lang.String) on null context object
)。您应该可以通过删除JSESSIONID
cookie来对此进行测试。
${session}
是SessionAttributesMap
,即使没有有效的会话,它似乎永远不会为null。在这种情况下,表达式${session.booleanAttribute}
仍然可以使用,只需评估为false。