美文网首页
Thymeleaf 模板语言

Thymeleaf 模板语言

作者: DimonHo | 来源:发表于2019-08-20 11:26 被阅读0次

    Thymeleaf内置对象

    一:基础对象

    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}
    
    1. vars: the context variables,访问VariablesMap所有上下文中的变量
    #org.thymeleaf.context.VariablesMap    
    ${#vars.get('foo')}    
    ${#vars.containsKey('foo')}    
    ${#vars.size()}
    
    1. locale: the context locale, java.util.Locale对象的访问

    2. request: (only in Web Contexts) the HttpServletRequest object

    3. response: (only in Web Contexts) the HttpServletResponse object

    4. session: (only in Web Contexts) the HttpSession object(需要contoller/action(HttpSession session))

    5. 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()}
    
    1. 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')}    
    
    1. session:访问session属性
    ${session.foo} // Retrieves the session atttribute 'foo'        
    ${session.size()}       
    ${session.isEmpty()}        
    ${session.containsKey('foo')}    
    
    1. 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>
    

    相关文章

      网友评论

          本文标题:Thymeleaf 模板语言

          本文链接:https://www.haomeiwen.com/subject/rznjgqtx.html