Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing session attributes in Thymeleaf templates

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.

like image 337
Deepak Ramakrishnan Kalidass Avatar asked Dec 17 '13 10:12

Deepak Ramakrishnan Kalidass


2 Answers

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

  • with a session variable:
    ${session.foo} // Retrieves the session atttribute 'foo'
    ${session.size()}
    ${session.isEmpty()}
    ${session.containsKey('foo')}
  • with a #ctx object:
    ${#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

like image 55
Rafal Borowiec Avatar answered Oct 13 '22 20:10

Rafal Borowiec


I tried this and it's working for me :

th:text="${session.user['tel']}"
like image 22
Abd Abughazaleh Avatar answered Oct 13 '22 20:10

Abd Abughazaleh