1.简介

Thymeleaf是一个视图技术。具有比JSP效率更高的优点,是Spring Boot官方推荐的视图技术。
Thymeleaf在学习时,就是在学习很多HTML标签属性。通过这些属性完成逻辑控制功能。例如:实现if、循环等。
所有Thymeleaf的属性都是通过th:进行使用。如果希望有提示,可以在<html xmlns:"http://www.thymeleaf.org">
Spring Boot整合Thymeleaf时只需要添加Spring-boot-start-thymeleaf启动器即可。
Thymeleaf页面扩展名.html

2.Thymeleaf简单使用介绍

Thymeleaf 基本用法总结
一、引用命名空间

    在html中引入此命名空间,可避免编辑器出现html验证错误,虽然加不加命名空间对Thymeleaf的功能没有任何影响。

二、输出内容

    2.1  <p th:text="#{home.welcome}">Welcome to our grocery store!</p>

    说明:

             1. th:text  用来将内容输出到所在标签的body中。

             2. #{home.welcome} 用来引入数据home对象中的 welcome属性。

             3. 可以用th:utext 用来显示“unescaped ” 的html内容。

    2.2    <p>Today is: <span th:text="${today}">13 February 2011</span></p>

    说明:${today} 用来引用 today 变量

三、访问对象

   ${param.x} 返回名为x 的 request参数。(可能有多个值)

   ${session.x} 返回名为x的Session参数。

   ${application.x} 返回名为 servlet context 的参数。

四、基本语法

   4.1  #{home.welcome} --  访问数据

   4.2  #{home.welcome(${session.user.name})}  -- 格式化数据 当 home.welcome 为 "abcdegf{0}"  类似这种内容时。(多个参数以逗句分隔)。

   4.3  ${today} --- 访问变量

   4.4  访问基本对象

ctx: the context object.

vars: the context variables.

locale: the context 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.

servletContext: (only in Web Contexts) the ServletContext object.

其它公共对象参考: http://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#appendix-a-expression-basic-objects

    4.5  日期的输出

    <span th:text="${#calendars.format(today,'dd MMMM yyyy')}">13 May 2011</span>

    4.6  星号语法

Name: Sebastian.

Surname: Pepper.

Nationality: Saturn.

4.7 输出URL

   <a href="product/list.html" th:href="@{/product/list}">Product List</a>

   <a href="details.html" th:href="@{/order/{orderId}/details(orderId=${o.id})}">view</a>

   4.8  使用代码段

   <div th:insert="~{commons :: main}">...</div>

   4.9  直接输出内容   

-- 输出字符

-- 输出数据表达式

--输出布尔表达式

-- 带变量的

4.10 条件表达式

... ...省略 false 结果的表达方式
...省略 true 结果的表达方式

Age: 27.

no user authenticated --不做任何处理时用下划线 _ 表示

4.11 格式化

   <td th:text="${{user.lastAccessDate}}">...</td> --${{.}}  调用默认的格式化器来输出结果。

   4.12  预处理

   <p th:text="${__#{article.text('textVar')}__}">Some text here...</p>  

   说明:thymeleaf 的处理模板内容的顺序与书写顺序无关,只能通过  __${expression}__ ,来将需要先一步计算出来后面          要用的变量指定为优化处理。

五、设置 Attribute 值

   5.1 设置任何Attribute 的方法

   <input type="submit" value="Subscribe!" th:attr="value=#{subscribe.submit}"/>   --设置单个

   <img src="../../images/gtvglogo.png"  th:attr="src=@{/images/gtvglogo.png},title=#{logo},alt=#{logo}" />  --一次设置多个

    5.2 设置一些内置的Attribute的方法   

   <li><a href="product/list.html" th:href="@{/product/list}">Product List</a></li>

   <form action="subscribe.html" th:action="@{/subscribe}">

   <input type="submit" value="Subscribe!" th:value="#{subscribe.submit}"/>

   <img src="../../images/gtvglogo.png"  th:src="@{/images/gtvglogo.png}" th:alt-title="#{logo}" /> -- 一次设置多个(alt title)的方法

   其它的可用属性:http://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#setting-value-to-specific-attributes

    5.3 设置html里没有指的任何属性的语法

    <span th:whatever="${user.name}">...</span>   ---whatever 可以换成任何你想设的属性

六、循环输出的语法

   6.1 基本循环
 <td th:text="${prod.name}">Onions</td>
 <td th:text="${prod.price}">2.41</td>
 <td th:text="${prod.inStock}? #{true} : #{false}">yes</td>

6.2 循环状态的使用

NAME PRICE IN STOCK
Onions 2.41 yes
   关于状态的其它信息的使用详细参考:http://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#keeping-iteration-status

七、条件判断

   7.1 if 和 unless

   <a href="comments.html" th:href="@{/comments(prodId=${prod.id})}" th:unless="${#lists.isEmpty(prod.comments)}">view</a>

   <a href="comments.html"  th:href="@{/product/comments(prodId=${prod.id})}"   th:if="${not #lists.isEmpty(prod.comments)}">view</a>

   7.2 switch 语句

User is an administrator

User is a manager

User is some other thing

--默认的 case 相当于default

八、模板 include

  8.1 定义和引用代码块

  定义代码块
© 2011 The Good Thymes Virtual Grocery

引用代码块

...

引用未用fragment 标注的代码块

© 2011 The Good Thymes Virtual Grocery

...

8.2 th:insert th:replace th:include 之间的区别

th:insert --- 插入代码块 th:replace -- 替换代码块会替换掉容器标签 th:include ---- 和insert相似但只会插入fragment标注body内的内容。

8.3 带参数的代码段

...

 <div th:replace="::frag (${value1},${value2})">...</div>
 <div th:replace="::frag (onevar=${value1},twovar=${value2})">...</div>

九、局部变量的使用示例

The name of the first person is Julius Caesar.

The name of the first person is Julius Caesar.

But the name of the second person is Marcus Antonius.

十、注释

    <!-- ... -->  

十一、说明

    以上只列出Thymeleaf了简要常用的语法和使用方式,更多详情的说明和规则请参见:http://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#introducing-thymeleaf