Thymeleaf模板引擎之内置对象

一、简介

         和JSP一样,Thymeleaf也是具有内置对象一说的,笔者在这里会进一步搜集和汇总thymeleaf常用的内置对象。为了能够进一步详细讲解这一段的使用方法,基于官网,并且翻阅同行前辈们的资料的基础上,详细汇编出这一章的内容,用于笔者技术水平有限,错误之处在所难免,敬请各位读者谅解和提出宝贵的意见和建议。

         主要的参考链接清单如下:

         资源链接1:https://www.jianshu.com/p/b750b8643a43

         资源链接2:https://blog.csdn.net/mygzs/article/details/52469770

         资源链接3:https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html

二、基础对象

1、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}
2、vars: the context variables
    访问VariablesMap所有上下文中的变量
    #org.thymeleaf.context.VariablesMap
    ${#vars.get('foo')}
    ${#vars.containsKey('foo')}
    ${#vars.size()}
3、locale: the context locale
    java.util.Locale对象的访问
Established locale country: <span th:text="${#locale.country}">US</span>
4、request: (only in Web Contexts) the HttpServletRequest object
5、response: (only in Web Contexts) the HttpServletResponse object
6、session: (only in Web Contexts) the HttpSession object( 需要contoller/action(HttpSession session) )
7、servletContext: (only in Web Contexts) the ServletContext object

三、web环境对象

1、#httpServletRequest :javax.servlet.http.HttpServletRequest对象实例
    ${#httpServletRequest.getAttribute('foo')}
    ${#httpServletRequest.getParameter('foo')}
    ${#httpServletRequest.getContextPath()}
    ${#httpServletRequest.getRequestName()}
2、#httpSession(需要contoller/action(HttpSession session))
    ${#httpSession.getAttribute('foo')}
    ${#httpSession.id}
    ${#httpSession.lastAccessedTime}

四、Spring环境对象

    #themes : 提供和“ spring:theme JSP tag.”同样的功能。
    ${#themes.code('foo')}

五、web环境中request/session等属性的使用

    1、param :获取请求的参数.
        ${param.foo} // Retrieves a String[] with the values of request parameter 'foo'
        ${param.size()}
        ${param.isEmpty()}
        ${param.containsKey('foo')}
    2、session:访问session属性
        ${session.foo} // Retrieves the session atttribute 'foo'
        ${session.size()}
        ${session.isEmpty()}
        ${session.containsKey('foo')}
    3、application:获取应用程序/ servlet上下文属性
        ${application.foo} // Retrieves the ServletContext atttribute 'foo'
        ${application.size()}
        ${application.isEmpty()}
        ${application.containsKey('foo')}

六、直接访问spring注册对象

<div th:text="${@authService.getUserName()}">

七、表达式实用程序对象

#dates : 为 java.util.Date对象提供工具方法,比如:格式化,提取年月日等;

#calendars : 类似于#dates , 但是只针对java.util.Calendar对象;

#numbers : 为数值型对象提供工具方法;

#strings :为String 对象提供工具方法。如: contains, startsWith, prepending/appending等;

#objects : 为object 对象提供常用的工具方法;

#bools : 为boolean 对象提供常用的工具方法;

#arrays : 为arrays 对象提供常用的工具方法;

#lists :为lists对象提供常用的工具方法;

#sets : 为sets对象提供常用的工具方法;

#maps : 为maps对象提供常用的工具方法;

#aggregates :为创造一个arrays 或者 collections聚集函数提供常用的工具方法;

#messages :用于在变量表达式中获得外部消息的实用方法,与使用#{...}语法获得的方式相同;

#uris:用于在Thymeleaf标准表达式中执行URI / URL操作(特别是转义/消除转义)的实用对象;

#conversions:允许在模板的任意位置执行转换服务的实用程序对象;

#ids : 为可能需要循环的ID属性提供常用的工具方法。





猜你喜欢

转载自blog.csdn.net/liubin5620/article/details/80481848