Thymeleaf模板引擎

一、简介

    thymeleaf是一种Java模板引擎,支持特定格式html、xml,js、css。特点原型即页面,支持静态访问。


二、标准方言

    1.变量表达式:${...}

          前端收到对象user取出name的属性

          <span th:text="${user.name}">

    2.消息表达式:${...}

           文本外部化

           <p th:text="#{header.address.city}"></p> 

    3.选择表达式:*{...}

            选择表达式是在当前选择的对象上执行而不是整个上下文

            <form action="/users" th:action="@{/users}" method="POST" th:object="${userModel.user}">

                 <input type="hidden" name="id" th:value="*{id}">

            </form>

             *{id} 同${userModel.user.id}

    4.链接表达式:@{...}

        url支持相对和绝对

        <a th:href="@{.../users/list}">...</a><a th:href="@{http://www.baidu.com}">...</a>

    5.分段表达式:th:insert th:replace th:include

        相当于插入

        <footer th:fragment="copy">

            <h1> hello </h1>

        </footer>

        #号分别代表insert、replace、include操作

        <div th:#="footer::copy"></div>

        th:insert结果:

            <div>

                <footer th:fragment="copy">

                    <h1> hello </h1>

                </footer>

            </div>

        把footer标签插入到div标签中

        th:replace结果:

            <footer th:fragment="copy">

                <h1> hello</h1>

            </footer>

         把div标签换成footer标签

        th:include结果

            <div>

                <h1> hello</h1>

            </div>

           把div标签里面的内容换成footer标签内容

    6.字面量

        字面量可以文本、数字、布尔和null等类型

    7.算术操作 +-*/%

        <div th:width="isEven=(${user.age}%2==0)">

    8.其他运算符

        比较 > < >= <= (gt lt ge le)

        等价 == != (eq ne)

        三目运算符

        <tr th:class="${row.even}?'even':'odd'"></tr>

    9.迭代器:th:each

        <tr th:each="user:${userList}">

            <td th:text="${user.id}"></td>

            <td th:text="${user.email}"></td></tr>

        </tr>

        迭代器状态变量

        index count size current even/odd first last

    10.条件语句 th:if th:unless switch

        <div th:switch="${user.role}">

            <p th:case="'admin'">user is guest</p>

            <p th:case="'guest'"> user is guest</p>

        </div>

    11.模板布局 th:fragment

        <div th:fragment="header">

            <h1> action </h1>

            <a href="/users"></a>

        </div>

        引用

        <div th:replace="~{fragments/header::header}"></div>

    12.表达式

         param session application request servletContext

ThymeleafJava模板引擎
提交需求
电话咨询
电话咨询:180-6574-6936