Thymeleaf内置对象
一:基础对象
- ctx: the context object
ctx对象继承org.thymeleaf.context.IContext
或者org.thymeleaf.context.IWebContext
,取决于当前环境是不是web环境。如果程序集成了spring,那么将会是
org.thymeleaf.spring[3|4].context.SpringWebContext
#org.thymeleaf.context.IContext
${#ctx.locale}
${#ctx.variables}
#org.thymeleaf.context.IWebContext
${#ctx.applicationAttributes}
${#ctx.httpServletRequest}
${#ctx.httpServletResponse}
${#ctx.httpSession}
${#ctx.requestAttributes}
${#ctx.requestParameters}
${#ctx.servletContext}
${#ctx.sessionAttributes}
- vars: the context variables,访问VariablesMap所有上下文中的变量
#org.thymeleaf.context.VariablesMap
${#vars.get('foo')}
${#vars.containsKey('foo')}
${#vars.size()}
-
locale: the context locale, java.util.Locale对象的访问
-
request: (only in Web Contexts) the HttpServletRequest object
-
response: (only in Web Contexts) the HttpServletResponse object
-
session: (only in Web Contexts) the HttpSession object(需要contoller/action(HttpSession session))
-
servletContext: (only in Web Contexts) the ServletContext object
二:web环境对象
- httpServletRequest :javax.servlet.http.HttpServletRequest对象实例
${#httpServletRequest.getAttribute('foo')}
${#httpServletRequest.getParameter('foo')}
${#httpServletRequest.getContextPath()}
${#httpServletRequest.getRequestName()}
- httpSession(需要contoller/action(HttpSession session))
${#httpSession.getAttribute('foo')}
${#httpSession.id}
${#httpSession.lastAccessedTime}
三:spring环境对象
themes : 提供和“ spring:theme JSP tag.”同样的功能。
${#themes.code('foo')}
四:web环境中访问request/session等属性
- param :获取请求的参数.
${param.foo} // Retrieves a String[] with the values of request parameter 'foo'
${param.size()}
${param.isEmpty()}
${param.containsKey('foo')}
- session:访问session属性
${session.foo} // Retrieves the session atttribute 'foo'
${session.size()}
${session.isEmpty()}
${session.containsKey('foo')}
- application:获取应用程序/ servlet上下文属性
${application.foo} // Retrieves the ServletContext atttribute 'foo'
${application.size()}
${application.isEmpty()}
${application.containsKey('foo')}
五:直接访问spring注册对象
<div th:text="${@authService.getUserName()}">
thymeleaf 页面获取当前页面的完整URL地址
下面两种方法是一样的
<div th:text="${#httpServletRequest.getRequestURL() + '?' + #httpServletRequest.getQueryString()}"></div>
<div th:text="${#httpServletRequest.requestURL + '?' + #httpServletRequest.queryString}"></div>
网友评论