主要目标是将优雅的自然模板带到开发工作流程中,并将HTML在浏览器中正确显示,并且可以作为静态原型,让开发团队能更容易协作。Thymeleaf能够处理HTML,xml,Js,css甚至纯文本。
Thymeleaf是原生的,不依赖于标签库。它能够在接受原始HTML的地方进行编辑和渲染。因为它没有与Servlet规范耦合,因此Thymeleaf模板能进入jsp所无法涉及的领域。
文档支持和页面原型支持;没法把jsp改成html,所以有了thymeleaf;所以产品经理所制定的页面后端程序员很难进行复用;画好的原型页面thymeleaf能够直接使用;所以有了模板引擎,thymeleaf>freemarker;【这是单体项目特点】
【分布式的,前后端分离的】thymeleaf就没有特点了;
××××××××××××××××××××××××基本使用thymeleaf
它的html页面不允许直接访问,需要浏览器进行渲染;
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
在运行时,springboot的自动配置会发现在Thymeleaf在类路径中,因为会为SpringMVC创建支撑Thymeleaf视图的bean。
像Thymeleaf这样的视图库在设计时与特定的web框架解耦的。这样的话它们就无法感知Spring的模型对象,因此无法与控制器放到Model中的数据协同工作。但是它可以与Servlet的request属性进行协作。所以,在Spring将请求转换到视图之前,它会把模型数据复制到request属性中,Thymeleaf和其他的视图模板就能够访问到它们了。
文本显示
<p th:text="${message}" > placeholder message </p>
循环
<div th:each="ingredient : ${wrap}">
<input name="gredients" type="checkbox" th:value="${ingredient.id}"/>
<span th:text="{ingredient.name}">Ingredient</span><br/>
</div>
链接
<link rel="stylesheet" th:href="@{/style.css}" />
<img th:src="@{/images/thymeleaf01.png}"/>
表单
<form method="post" th:object="${design}">
<span th:text=”pshd”>hx</span> <span th:”${msg}”> 从controller来 | 输出:pshd,标签中的内容会被替换 输出了变量的值; |
Th:value | 可将一个值放入到input的标签的value中 |
内置对象:可以在模板中直接使用,用# |
|
${#strings.isEmpth(key)} | 判断字符串是否为空ture/false |
${#strings.contains(msg,’T’)} | 判断字符串是够包含指定字符串 |
${strings.strtsWith(msg,’a’)} | 判断当前串是否以子串开头 |
${#strings.length(msg)} | 返回字符串长度 |
${#strings.indexOf(msg,’h’)} | 查找子串的位置,返回其下标/-1 |
${#strings.substring(msg,2)} ${#strings.substring(msg,2,5)} |
|
${#dates.format(key)} | 格式化日期为浏览器默认格式标准 |
${#dates.format(key,’yyyy/MM/dd’)} ${#dates.year(key)} ${#dates.month(key)} ${#dates.day(key)} |
|
th:if | 条件判断 |
th:switch/th:case | 多个匹配,显示第一条配置;没有default,但是th:case=”*”等同 |
Th:each 状态变量: Index:当前迭代器的索引从0开始 Count:当前迭代对象的计数从1开始 Size:被迭代对象的长度 Odd/even:布尔值,当前循环是否是偶数/奇数 从0开始 First:布尔值,当前循环的是否是第一条 Last:布尔值,当前逊汗的是否是最后一条 | 迭代器,用户循环迭代集合 |
|
|
<span th:text=”${#strings.isEmpty(msg)}”></span>
Model.addAttribute(“sex”,”男”);
<div>
<span th:if=”${sex}==’男’”>
性别:男
</span>
<span th:if=”${sex}==’女’”>
性别:女
</span>
</div>
<div th:switch=”${id}”>
<span th:case=”1”>
ID为1的内容
</span>
</div>
<table border=”1” >
<tr>
<th>ID</th>
<th>Name</th>
<th>age</th>
</tr>
<tr th:each=”u : ${lisst}”>
<td th:text=”${u.id}”></td>
<td th:text=”${u.Name}”></td>
<td th:text=”${u.age}”></td>
</tr>
</table>
Th:each 迭代Map
<table>
<tr>
<th>Value</th>
</tr>
<tr th:each=”m : ${map}”>
<td th:text=”${m}”></td> 输出的是对象的地址是entry类型
</tr>
<tr th:each=”m :${map}”>
<td th:text=”${m.value.id}”></td>
<td th:text=”${m.value.name}”></td>
<td th:text=”${m.value.age}”></td>
</tr>
</table>
×××××××××××××××××××操作域对象
Request.setAttribute(“req”,”HttpServletRequest”);
Request.getSesson().setAttribute(“ses”,”httpSession”);
Request.getSession().getServletContext().setAttribute(“app”,”application”)
HttpServletRequest
Request.setAttribute(“req”,”HttpServletRequest”);
<span th:text=”${#httpServletRequest.getAttribute(‘req’)}”></span>
<span th:text=”${#request.getAttribute(‘req’)}”></span>
HttpSession
Request.getSession.setAttribute(“ses”,”HttpSession”);
<span th:text=”${#session.sess}”></span><br/>
<span th:text=”${#session.getAttribute(‘ses’)}”></span>
springboot支持模板的方案
模板 | springboot的starter的依赖 |
FreeMarker | spring-boot-starter-freemarker |
Groovy Templates | spring-boot-starter-groovy-templates |
java server page | 由tomcat和jetty提供 |
Mustache | spring-boot-starter-mustache |
Thymeleaf | spring-boot-starter-thymeleaf |
缓存模板
默认情况下,模板只有在第一次使用的时候解析一次,解析的结果会被后续的请求所使用。对于生产环境来说,这是一个很棒的特性,它能防止每次请求时多余的模板解析过程,因此有助于提升性能。但是,在开发期就太不好了,无法看到页面的修改效果。因此有一种方法可以禁用缓存。我们只需在application.properties中添加spring.thymeleaf.cache=false。唯一需要注意的是,在将应用部署到生产环境之前,这一行代码一定要删除。另一种更简单的方式是单独配置一个测试环境的profile,打包时改成正式环境的配置文件即可。
模板 | 启用缓存的属性 |
FreeMarker | spring.freemarker.cache |
Groovy Templates | spring.groovu.template.chche |
Mustache | spring.mustache.cache |
Thymeleaf | spring.thymeleaf.chche |