
Thymeleaf Tutorial: Chapter 18. Appendix A: Basic Expressions
- Tutorial
Table of contents
Some objects and variables are always available for invocation. Let's look at them:
Base
#ctx objects : a context object. Implementation of org.thymeleaf.context.IContext or org.thymeleaf.context.IWebContext depending on our environment (standalone or web).
Note #vars and #root are synonyms for the same object, but #ctx is recommended .
#locale : direct access to java.util.Locale associated with the current request.
Web context namespaces for request / session attributes, etc.
When using Thymeleaf in a web environment, we can use a number of shortcuts to access request parameters, session attributes, and application attributes:
Note that these are not context objects, but map added to the context as variables, so we access them without #. In a way, they act like namespaces.
param : to get request parameters. $ {param.foo} is a String [] with the request parameter values foo, so $ {param.foo [0]} is usually used to get the first value.
session : to get session attributes.
application : to retrieve the application / servlet context attributes.
Note : there is no need to specify a namespace to access request attributes (unlike request parameters), since all request attributes are automatically added to the context as variables in the context root:
Web Context Objects
Inside the web environment, there is also direct access to the following objects (note that these are objects, not maps / namespaces):
#request : direct access to the javax.servlet.http.HttpServletRequest object associated with the current request.
#session : direct access to the javax.servlet.http.HttpSession object associated with the current request.
#servletContext : direct access to the javax.servlet.ServletContext object associated with the current request.
18 Appendix A: Basic Expressions
Some objects and variables are always available for invocation. Let's look at them:
Base
#ctx objects : a context object. Implementation of org.thymeleaf.context.IContext or org.thymeleaf.context.IWebContext depending on our environment (standalone or web).
Note #vars and #root are synonyms for the same object, but #ctx is recommended .
/*
* ==============================
* См. Javadoc API для класса org.thymeleaf.context.IContext
* ==============================
*/
${#ctx.locale}
${#ctx.variableNames}
/*
* ==============================
* См. Javadoc API для класса org.thymeleaf.context.IWebContext
* ==============================
*/
${#ctx.request}
${#ctx.response}
${#ctx.session}
${#ctx.servletContext}
#locale : direct access to java.util.Locale associated with the current request.
${#locale}
Web context namespaces for request / session attributes, etc.
When using Thymeleaf in a web environment, we can use a number of shortcuts to access request parameters, session attributes, and application attributes:
Note that these are not context objects, but map added to the context as variables, so we access them without #. In a way, they act like namespaces.
param : to get request parameters. $ {param.foo} is a String [] with the request parameter values foo, so $ {param.foo [0]} is usually used to get the first value.
/*
* ==============================
* См. Javadoc API для класса org.thymeleaf.context.WebRequestParamsVariablesMap
* ==============================
*/
${param.foo} // Возвращает строку [] со значениями параметра запроса 'foo'
${param.size()}
${param.isEmpty()}
${param.containsKey('foo')}
session : to get session attributes.
/*
* ==============================
* См. Javadoc API для класса org.thymeleaf.context.WebSessionVariablesMap
* ==============================
*/
${session.foo} // Возвращает сеанс atttribute 'foo'
${session.size()}
${session.isEmpty()}
${session.containsKey('foo')}
application : to retrieve the application / servlet context attributes.
/*
* ==============================
* См. Javadoc API для класса org.thymeleaf.context.WebServletContextVariablesMap
* ==============================
*/
${application.foo} // Получает ServletContext аттрибут 'foo'
${application.size()}
${application.isEmpty()}
${application.containsKey('foo')}
Note : there is no need to specify a namespace to access request attributes (unlike request parameters), since all request attributes are automatically added to the context as variables in the context root:
${myRequestAttribute}
Web Context Objects
Inside the web environment, there is also direct access to the following objects (note that these are objects, not maps / namespaces):
#request : direct access to the javax.servlet.http.HttpServletRequest object associated with the current request.
${#request.getAttribute('foo')}
${#request.getParameter('foo')}
${#request.getContextPath()}
${#request.getRequestName()}
#session : direct access to the javax.servlet.http.HttpSession object associated with the current request.
${#session.getAttribute('foo')}
${#session.id}
${#session.lastAccessedTime}
#servletContext : direct access to the javax.servlet.ServletContext object associated with the current request.
${#servletContext.getAttribute('foo')}
${#servletContext.contextPath}