一、常见的模板引擎
jsp、freemarker
、
velocity、themleaf等......
二、什么是模板引擎?
模板引擎是为了解决用户界面(显示)与业务数据(内容)分离而产生的。 他可以生成特定格式的文档,常用的如格式如HTML
、
xml
以及其他格式的文本格式。
三、认识Thymeleaf
Thymeleaf是用来开发Web和独立环境项目的服务器端的Java
模版引擎
Spring官方支持的服务的渲染模板中,并不包含jsp。而是Thymeleaf和 Freemarker等,而Thymeleaf与SpringMVC的视图技术,及SpringBoot的 自动化配置集成非常完美,几乎没有任何成本,你只用关注Thymeleaf的语法即可。
四、Thymeleaf常用语法的指令
1.th:text
计算其值表达式并将结果设置为标签的标签体
<p th:text="${userName}">中国</p>,值为 null 为空时,整个标签不显示任何内容
2.th:utext
th:text 会对结果中的特殊字符转义,th:utext 不会转义
<p th:utext="${userInfo}">中国</p>,值为 null 为空时,整个标签不显示任何内容。
3.th:classappend
为标签中的任意属性设置,可以一次设置多个属性
<a href=“” th:classappend=“classname1”>删除</a>
4.th:fragment
定义模板片段
<div th:fragment="copy">
5.th:insert
将被引用的模板片段插⼊到自己的标签体中
<div th:insert="~{footer :: copy}"></div>
6.th:replace
将被引用的模板片段替换掉自己
<div th:replace="footer :: copy"></div>
7.th:include
类似于 th:insert,⽽不是插⼊⽚段,它只插⼊此 ⽚段的内容
<div th:include="footer :: copy"></div>
8.th:remove
删除模板中的某些代码片段
<tr th:remove="all">
9.th:each
迭代数据,如 数组、List、Map 等
<tr th:each="user : ${userList}">
10.th:if
条件为 true 时,显示模板⽚段,否则不显示
<p th:if=“${istrue}”>你好</p>
11.th:unless
条件为 false 时,显示模板⽚段,否则不显示
<p th:unless=“!${istrue}”>你好</p>
12.th:switch
与 Java 中的 switch 语句等效,有条件地显示匹
配的内容
<div th:switch="1">
13.th:case
配合 th:switch 使用
<div th:switch="1">
<p th:case="0">管理员</p>
<p th:case="1">操作员</p>
<p th:case="*">未知用户</p>
</div>