spring 访问 Thymeleaf 模板中的会话属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20632134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 06:25:17  来源:igfitidea点击:

Accessing session attributes in Thymeleaf templates

springsession-variablesthymeleaf

提问by Deepak Ramakrishnan Kalidass

I would like to know whether is it possile to retrieve the session object and access its attributes from a Thymeleaf template without any controller code.

我想知道是否可以在没有任何控制器代码的情况下从 Thymeleaf 模板中检索会话对象并访问其属性。

回答by Rafal Borowiec

In Thymeleaf, session object can be easily accessed in a template:

在 Thymeleaf 中,可以在模板中轻松访问会话对象:

  • with a sessionvariable:
  • 带有session变量:
    ${session.foo} // Retrieves the session atttribute 'foo'
    ${session.size()}
    ${session.isEmpty()}
    ${session.containsKey('foo')}
  • with a #ctxobject:
  • 有一个#ctx对象:
    ${#ctx.httpSession}

Look at the Thymeleaf documentation for accessing different context objects: http://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#expression-basic-objects

查看用于访问不同上下文对象的 Thymeleaf 文档:http: //www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#expression-basic-objects