Thymeleaf简介
Thymeleaf是什么? 简单的说它是一个类似于Freemarker和Velocity的模板,它可以完全的替代JSP。
Thymeleaf在有网络和无网络的环境下皆可运行,它可以让前端工程师在浏览器查看页面的静态效果,也可以让后端工程师在服务器查看带数据的动态页面效果,这是由于它支持 html 原型,然后在 html 标签里增加额外的属性来达到模板+数据的展示方式。浏览器解释 html 时会忽略未定义的标签属性,所以 thymeleaf 的模板可以静态地运行;当有数据返回到页面时,Thymeleaf 标签会动态地替换掉静态内容,使页面动态显示。
用例测试
1、引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2、配置文件
#设置后缀以及页面存放位置
spring:
thymeleaf:
prefix: classpath:/web/
suffix: .html
3、Controller类
@Controller
@RequestMapping("/index/")
public class StudentController {
@RequestMapping("test")
public String test(Model model) {
//传入字符串
model.addAttribute("test","Hellow Spring boot Freemarker!!");
//传入对象
Student stu = new Student(101, "张三");
model.addAttribute("stu",stu);
//传入集合
List<Object> list = new ArrayList<Object>();
list.add(new Student(102, "李四"));
list.add(new Student(103,"王五"));
model.addAttribute("list",list);
return "student";
}
}
4、页面
<body>
<h2>thymeleaf</h2><br>
<!-- 渲染文本 -->
<span th:text="${test}"></span><br>
<!-- 对象 -->
<span th:text="${stu.id}"></span><br>
<span th:text="${stu.name}"></span><br>
<!-- 替换超链接 (带参数) -->
<a href="https://www.zy.com" th:href="@{https://www.baidu.com(id=${stu.id})}">带参链接</a><br>
<!-- 替换超链接 (无参数) -->
<a href="https://www.zy.com" th:href="@{https://www.baidu.com}">无参链接</a><br>
<!-- 循环 -->
<ul>
<li th:each="s:${list}" th:text="${s.name}"></li>
</ul>
</body>
项目结构
浏览器输入:http://localhost:8080/index/test
测试结果
常用的th标签
关键字 | 功能介绍 | 案例 |
th:id | 替换id | <input th:id="'xxx' + ${collect.id}"/> |
th:text | 文本替换 | <p th:text="${collect.description}">description</p> |
th:utext | 支html的文本替换 | <p th:utext="${htmlcontent}">conten</p> |
th:object | 替换对象 | <div th:object="${session.user}"> |
th:value | 属性赋值 | <input th:value="${user.name}" /> |
th:with | 变量赋值运算 | <div th:with="isEven=${prodStat.count}%2==0"></div> |
th:style | 设置样式 | th:style="'display:' + @{(${sitrue} ? 'none' : 'inline-block')} + ''" |
th:onclick | 点击事件 | th:οnclick="'getCollect()'" |
th:each | 属性赋值 | tr th:each="user,userStat:${users}"> |
th:if | 判断条件 | <a th:if="${userId == collect.userId}" > |
th:unless | 和th:if判断相反 | <a th:href="@{/login}" th:unless=${session.user != null}>Login</a> |
th:href | 链接地址 | <a th:href="@{/login}" th:unless=${session.user != null}>Login</a> /> |
th:switch | 多路选择 配合th:case 使用 | <div th:switch="${user.role}"> |
th:case | th:switch的一个分支 | <p th:case="'admin'">User is an administrator</p> |
th:fragment | 布局标签,定义一个代码片段,方便其它地方引用 | <div th:fragment="alert"> |
th:include | 布局标签,替换内容到引入的文件 | <head th:include="layout :: htmlhead" th:with="title='xx'"></head> /> |
th:replace | 布局标签,替换整个标签到引入的文件 | <div th:replace="fragments/header :: title"></div> |
th:selected | selected选择框 选中 | th:selected="(${xxx.id} == ${configObj.dd})" |
th:src | 图片类地址引入 | <img class="img-responsive" alt="App Logo" th:src="@{/img/logo.png}" /> |
th:inline | 定义js脚本可以使用变量 | <script type="text/javascript" th:inline="javascript"> |
th:action | 表单提交的地址 | <form action="subscribe.html" th:action="@{/subscribe}"> |
th:remove | 删除某个属性 | <tr th:remove="all"> |