📜 ⬆️ ⬇️

Thymeleaf Tutorial: Chapter 18. Appendix A: Basic Expressions

Table of contents

18 Appendix A: Basic Expressions


Some objects and variables are always available for calling. Let's look at them:

Basic objects
')
#ctx : context object. The 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 maps added to the context as variables, so we refer to them without #. In some ways they act as namespaces.

param : to get request parameters. $ {param.foo} is a String [] with the values ​​of the query parameter 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 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 the namespace to access the request attributes (as opposed to the 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} 

Source: https://habr.com/ru/post/352552/


All Articles