zoukankan      html  css  js  c++  java
  • Thymeleaf 基本表达式

    Thymeleaf 基本表达式

     

    如需了解thymeleaf以及thymeleaf整合spring,请参考《Thymeleaf模板引擎使用》、《Thymeleaf 集成spring》

     

    ${}

    变量表达式(美元表达式,哈哈),用于访问容器上下文环境中的变量,功能同jstl中${}。

    例如:

    复制代码
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) 
    
    throws ServletException, IOException {
    
    ...
    
    //Create Servlet context
    
    WebContext ctx = new WebContext(req, resp, this.getServletContext(), req.getLocale());
    
    ctx.setVariable("helloword","hello thymeleaf,wellcome!");
    
    //Executing template engine
    
    templateEngine.process("home", ctx, resp.getWriter());
    
    }
    复制代码

    模板页面访问变量

    <p><span th:text="${helloword}"></span></p>

     

    *{}

    选择表达式(星号表达式)。选择表达式与变量表达式有一个重要的区别:选择表达式计算的是选定的对象,而不是整个环境变量映射。也就是:只要是没有选择的对象,选择表达式与变量表达式的语法是完全一样的。那什么是选择的对象呢?是一个:th:object对象属性绑定的对象。

    例如:

    复制代码
    <div th: obj ect=" ${session. user}" >
    
    <p>Name: <span th: text=" *{firstName}" >Sebastian</span>. </p>
    
    <p>Surname: <span th: text=" *{lastName}" >Pepper</span>. </p>
    
    <p>Nationality: <span th: text=" *{nationality}" >Saturn</span>. </p>
    
    </div>
    复制代码

    上例中,选择表达式选择的是th:object对象属性绑定的session. user对象中的属性。

     

    #{}

    消息表达式(井号表达式,资源表达式)。通常与th:text属性一起使用,指明声明了th:text的标签的文本是#{}中的key所对应的value,而标签内的文本将不会显示。

    例如:

    新建/WEB-INF/templates/home.html,段落

    <p th: text=" #{home. welcome}" >This text will not be show! </p>

    新建/WEB-INF/templates/home.properties,home.welcome:

    home.welcome=this messages is from home.properties!

    测试结果:

    从测试结果可以看出,消息表达式通常用于显示页面静态文本,将静态文本维护在properties文件中也方面维护,做国际化等。

     

    @{}

    超链接url表达式。

    例如:

    <script th:src="@{/resources/js/jquery/jquery.json-2.4.min.js}"

     

    #maps

    工具对象表达式。常用于日期、集合、数组对象的访问。这些工具对象就像是java对象,可以访问对应java对象的方法来进行各种操作。

    例如:

    复制代码
    <div th:if="${#maps.size(stuReqBean.students[__${rowStat.index}__].score) != 0}">
    
    <label>${score.key}:</label><input type="text" th:value="${score.value}"></input>
    
    </div>
    
    <div th:if="${#maps.isEmpty(stuReqBean.students[__${rowStat.index}__].score)}">
    
    ...do something...
    
    </div>
    复制代码

    其他工具对象表达式还有:

    复制代码
    #dates
    
    #calendars
    
    #numbers 
    
    #strings
    
    #objects
    
    #bools
    
    #arrays
    
    #lists
    
    #sets
    复制代码

    更多详细表达式请访问:http://www.thymeleaf.org

     

  • 相关阅读:
    C#执行异步操作的几种方式
    spring boot swagger ui使用 nginx 部署后无法使用问题
    PHP支付宝接口RSA验证
    认证加密算法php hash_hmac和java hmacSha1的问题
    如何实现从 Redis 中订阅消息转发到 WebSocket 客户端
    ffmpeg安装
    nginx静态资源反向代理
    Redis基础知识 之——发布/订阅
    linux下安装git
    php实现无限级分类查询(递归、非递归)
  • 原文地址:https://www.cnblogs.com/ninsh1992/p/6269095.html
Copyright © 2011-2022 走看看