Thymeleaf是一种类似于JSP的动态网页技术
JSP 必须依赖Tomcat运行,不能直接运行在浏览器中
HTML可以直接运行在浏览器中,但是不能接收控制器传递的数据
Thymeleaf是一种既保留了HTML的后缀能够直接在浏览器运行的能力、又实现了JSP显示动态数据的功能——静能查看页面效果、动则可以显示数据
SpringBoot应用对Thymeleaf提供了良好的支持
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-thymeleafartifactId>
- dependency>
Thymeleaf模板就是HTML文件
SpringBoot应用中 resources\templates目录就是用来存放页面模板的
重要说明:
static 目录下的资源被定义静态资源,SpringBoot应用默认放行;如果将HTML页面创建static目录是可以直接访问的
templates 目录下的文件会被定义为动态网页模板,SpringBoot应用会拦截templates中定义的资源;如果将HTML文件定义在templates目录,则必须通过控制器跳转访问。
在templates创建HTML页面模板
创建PageController,用于转发允许"直接访问"的页面请求
- @Controller
- @RequestMapping("/page")
- public class PageController {
-
- @RequestMapping("/test.html")
- public String test(){
- return "test";
- }
-
- }
如果要在thymeleaf模板中获取从控制传递的数据,需要使用th标签
- html>
- <html lang="en" xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8">
- <title>Titletitle>
- head>
- <body>
-
- body>
- html>
在几乎所有的HTML双标签都可以使用 th:text属性,将接收到的数据显示在标签的内容中
- <label th:text="${price}">label>
- <div th:text="${str}">div>
- <p th:text="${book.bookName}">p>
HTML内联
<p th:inline="text">图书名称:[[${book.bookName}]]p> CSS内联
- <style type="text/css" th:inline="css">
- .style1{
- color:[[${color}]]
- }
- style>
JavaScript内联
- <script type="css/javascript" th:inline="javascript">
-
- script>
- <div th:object="${book}">
- <p th:text="*{bookId}">p>
- <p th:text="*{bookName}">p>
- <p th:text="*{bookAuthor}">p>
- div>
- <table style="width: 600px" border="1" cellspacing="0">
- <caption>图书信息列表caption>
- <thead>
- <tr>
- <th>图书IDth>
- <th>图书名称th>
- <th>作者th>
- tr>
- thead>
- <tbody>
- <tr th:each="b:${books}">
- <td th:text="${b.bookId}">td>
- <td th:text="${b.bookName}">td>
- <td th:text="${b.bookAuthor}">td>
- tr>
- tbody>
- table>
th:if 如果条件不成立,则不显示此标签
- <td th:if="${b.bookPrice}>40" style="color:red">太贵!!!td>
- <td th:unless="${b.bookPrice}>40" style="color:red">太贵!!!td>
-
- <td th:if="${b.bookPrice}<=40" style="color:green">推荐购买td>
th:switch 和 th:case
- <td th:switch="${b.bookPrice}/10">
- <label th:case="3">建议购买label>
- <label th:case="4">价格合理label>
- <label th:case="*">价格不合理label>
- td>
- <td th:switch="${user.gender}">
- <label th:case="M">男label>
- <label th:case="F">女label>
- <label th:case="*">性别不详label>
- td>
碎片,就是HTML片段,我们可以将多个页面中使用的相同的HTML标签部分单独定义,然后通过th:include可以在HTML网页中引入定义的碎片
定义碎片 th:fragment
header.html
- html>
- <html lang="en" xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8">
- <title>Titletitle>
- head>
- <body>
-
- <div th:fragment="fragment1" style="width: 100%; height: 80px;background: deepskyblue; color:white; font-size: 25px; font-family:文鼎霹雳体">
- 千锋武汉Java2010班,六六六!!!
- div>
-
- body>
- html>
footer.html
- html>
- <html lang="en" xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8">
- <title>Titletitle>
- head>
- <body>
-
- <div th:fragment="fragment2" style="width: 100%; height: 30px;background: lightgray; color:white; font-size: 16px;">
- 千锋教育 武汉校区
- div>
-
- body>
- html>
引用碎片 th:include 和 th:replace
a.html
- html>
- <html lang="en" xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8">
- <title>Titletitle>
- head>
- <body>
-
- <div th:replace="header::fragment1">div>
-
- <div style="width: 100%; height: 500px">
- 定义内容
- div>
-
- <div th:replace="footer::fragment2">div>
- body>
- html>