访问Thymeleaf模板中的会话属性

Dee*_*ass 8 spring session-variables thymeleaf

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

Raf*_*iec 20

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

  • session变量:
    ${session.foo} // Retrieves the session atttribute 'foo'
    ${session.size()}
    ${session.isEmpty()}
    ${session.containsKey('foo')}
  • 用一个#ctx对象:
    ${#ctx.httpSession}

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