- Thymeleaf是一款用于渲染XML/XHTML/HTML5内容的模板引擎。类似JSP,Velocity,FreeMaker等,它也可以轻易的与Spring MVC等Web框架进行集成作为Web应用的模板引擎。与其它模板引擎相比,Thymeleaf最大的特点是能够直接在浏览器中打开并正确显示模板页面,而不需要启动整个Web应用
- JSP在内嵌的Servlet的容器上有一些问题(内嵌Tomcat、Jetty不支持以jar形式运行JSP,Undertow不支持JSP),Spring boot提供大量模板引擎,包括FreeMarker、Groovy、Thymeleaf、Vilocity和Mustache,Spring Boot中推荐使用Thymeleaf作为模板引擎,因为Thymeleaf完美的支持Spring MVC.
- Thymeleaf是一个Java类库,它是一个xml/xthml/html5的模板引擎,可以作为MVC的web应用的view层
- Thymeleaf还提供了额外的模块与Spring MVC集成,所以我们可以使用Thymeleaf完全替代JSP
- 简单说, Thymeleaf 是一个跟 Velocity、FreeMarker 类似的模板引擎,它可以完全替代 JSP 。相较与其他的模板引擎,它有如下三个极吸引人的特点:
- 1.Thymeleaf 在有网络和无网络的环境下皆可运行,即它可以让美工在浏览器查看页面的静态效果,也可以让程序员在服务器查看带数据的动态页面效果。这是由于它支持 html 原型,然后在 html 标签里增加额外的属性来达到模板+数据的展示方式。浏览器解释 html 时会忽略未定义的标签属性,所以 thymeleaf 的模板可以静态地运行;当有数据返回到页面时,Thymeleaf 标签会动态地替换掉静态内容,使页面动态显示。
- 2.Thymeleaf 开箱即用的特性。它提供标准和spring标准两种方言,可以直接套用模板实现JSTL、 OGNL表达式效果,避免每天套模板、改jstl、改标签的困扰。同时开发人员也可以扩展和创建自定义的方言
- 3.Thymeleaf 提供spring标准方言和一个与 SpringMVC 完美集成的可选模块,可以快速的实现表单绑定、属性编辑器、国际化等功能。
- 基础知识
- 引入Thymeleaf
- 通过<html xmlns:th="http://www.Thymeleaf.org">,将镜头页面转换为动态的视图,需要进行动态处理的元素将使用“th”为前缀;
- 通过“@{}”引用Web静态资源
- 访问modle中的数据
- 通过“${}”访问modle中的属性
- 例如,<span th:text="${singlePerson.name}"></span>访问singlePerson的name属性。注意:需要处理的动态内容需要加上"th"前缀
- modle中的数据迭代
- 使用th:each来做循环迭代(th:each="person:${people}"),person作为迭代元素来使用,然后通过“${}”访问modle中的属性
- 数据判断
- 通过${not #lists.isEmpty(people)}表达式判断是否为空。Thymeleaf支持>、<、>=、<=、==、!=作为比较条件,同时也支持SpringEL表达式语言用于条件中。
- 在JavaScript中访问modle
- 通过 <script th:inline="javascript">添加到script标签,这样JavaScript代码即可访问Modle中的属性
- [[${}]]格式获得实际值,如var single=[[${singlePerson}]]; name=single.name;
- 选择(星号)表达式
- 选择表达式很像变量表达式,不过它们用一个预先选择的对象来代替上下文变量容器(map)来执行,如下: *{customer.name}
- Thymeleaf的一些主要标签和函数
- th:text,显示文本
- th:utext:和th:text的区别是针对"unescaped text"
- th:attr:设置标签属性
- th:if or th:unless:条件判断语句
- th:each:循环语句
- th:style:设置样式
- th:onclick:点击事件
- th:href:链接地址
- th:inline:定义js脚本可以使用变量
- th:action:表单提交地址
- #dates:日期函数
- #calendars:日历函数
- #numbers:数字函数
- #strings:字符串函数
- #objects:对象函数
- #bools:逻辑函数
- #arrays:数组函数
- #lists:列表函数
- iterStat称作状态变量,其属性有
- index:当前迭代对象的index(从0开始计算)
- count:当前迭代对象的index(从1开始计算)
- size:被迭代对象的大小
- current:当前迭代变量
- even/odd:布尔值,当前循环是否是偶数/奇数(从0开始计算)
- first:布尔值,当前循环是否是第一个
- last:布尔值,当前循环是否是最后一个
- 引入Thymeleaf
- Thymeleaf主要以属性的方式加入到html标签中,浏览器在解析html时,当检查到没有的属性时会忽略,所以Thymeleaf的模板可以通过浏览器直接打开,这样非常有利前后端的分离。
- Thymeleaf的默认参数配置
- 如有需要修改默认配置的时候,只需复制下面要修改的属性到application.properties中,并修改成需要的值,如修改模板文件的扩展名,修改默认的模板路径等。
- # Enable template caching.
- spring.thymeleaf.cache=true
- # Check that the templates location exists.
- spring.thymeleaf.check-template-location=true
- # Content-Type value.
- spring.thymeleaf.content-type=text/html
- # Enable MVC Thymeleaf view resolution.
- spring.thymeleaf.enabled=true
- # Template encoding.
- spring.thymeleaf.encoding=UTF-8
- # Comma-separated list of view names that should be excluded from resolution.
- spring.thymeleaf.excluded-view-names=
- # Template mode to be applied to templates. See also StandardTemplateModeHandlers.
- spring.thymeleaf.mode=HTML5
- # Prefix that gets prepended to view names when building a URL.
- spring.thymeleaf.prefix=classpath:/templates/
- # Suffix that gets appended to view names when building a URL.
- spring.thymeleaf.suffix=.html spring.thymeleaf.template-resolver-order= # Order of the template resolver in the chain. spring.thymeleaf.view-names= # Comma-separated list of view names that can be resolved.
- 在web项目中会使用如下配置
- spring:
- thymeleaf:
- prefix:/WEB-INF/views/
- suffix:.html
- mode:HTML5
- encoding:UTF-8
- content-type:text/html
- cache:false
- thymeleaf:
- 其中,prefix指定了HTML文件存放在webapp的/WEB-INF/views/目录下面,或者也可以指定其他路径
- spring:
Thymeleaf模板引擎基础知识
最新推荐文章于 2025-04-27 19:00:00 发布