Expession Basic Objects
Kontextvariabeln
Die folgenden Context-Variables werden von OGNL zur Verfügung gestellt:
- #ctx : the context object.
- #vars: the context variables.
- #locale : the context locale.
- #httpServletRequest : (only in Web Contexts) the HttpServletRequest object.
- #httpSession : (only in Web Contexts) the HttpSession object.
Beispiel:
Established locale country: <span th:text="${#locale.country}">US</span>.
ctx
#ctx : the context object. It will be an implementation of org.thymeleaf.context.IContext , org.thymeleaf.context.IWebContext depending on our environment (standalone or web). If we are using the Spring integration module, it will be an instance of org.thymeleaf.spring[3|4].context.SpringWebContext .
Verwendung:
${#ctx.applicationAttributes}
${#ctx.httpServletRequest}
${#ctx.httpServletResponse}
${#ctx.httpSession}
${#ctx.requestAttributes}
${#ctx.requestParameters}
${#ctx.servletContext}
${#ctx.sessionAttributes}
locale
direct access to the java.util.Locale associated with current request.
Verwendung:
${#locale}
vars
#vars : an instance of org.thymeleaf.context.VariablesMap with all the variables in the Context
Unqualified expressions are evaluated against this object. In fact, ${something} is completely equivalent to (but more beautiful than) ${#vars.something} . \root is a synomyn for the same object.
Verwendung:
${#vars.get('foo')}
${#vars.containsKey('foo')}
${#vars.size()}
param
for retrieving request parameters. ${param.foo} is a String[] with the values of the foo request parameter, so ${param.foo[0]} will normally be used for getting the first value.
Verwendung:
${param.foo} // Retrieves a String[] with the values of request parameter 'foo'
${param.size()}
${param.isEmpty()}
${param.containsKey('foo')}
Session
for retrieving session attributes.
Verwendung:
${session.foo} // Retrieves the session atttribute 'foo'
${session.size()}
${session.isEmpty()}
${session.containsKey('foo')}
Application
: for retrieving application/servlet context attributes.
Verwendung:
${application.foo} // Retrieves the ServletContext atttribute 'foo'
${application.size()}
${application.isEmpty()}
${application.containsKey('foo')}
httpServletRequest
direct access to the javax.servlet.http.HttpServletRequest object associated with the current request.
Verwendung:
${#httpServletRequest.getAttribute('foo')}
${#httpServletRequest.getParameter('foo')}
${#httpServletRequest.getContextPath()}
${#httpServletRequest.getRequestName()}
httpSession
direct access to the javax.servlet.http.HttpSession object associated with the current request.
Mit Spring sind auch die folgenden Objekte verfügbar:
themes
provides the same features as the Spring spring:theme JSP tag.
Verwendung:
${#themes.code('foo')}
Spring beans
Thymeleaf also allows accessing beans registered at your Spring Application Context in the standard way defined by Spring EL, which is using the syntax @beanName , for example:
<div th:text="${@authService.getUserName()}">...</div>