在springboot中使用Thymeleaf模板
常用命名空间
1 | xmlns:th="http://www.thymeleaf.org" |
步骤
在pom中引入
1
2
3
4<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
</dependency>在
resources/templates
下新建html文件(注意:这个路径是约定)在文件中引入thymeleaf命名空间
xmlns:th="http://www.thymeleaf.org"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<html
lang="en"
xmlns:th="http://www.thymeleaf.org"
>
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--所有的html元素都可以被thymeleaf替换, th:元素名-->
<h1 th:text="${msg}"></h1>
</body>
</html>Controller测试
1
2
3
4
5
6
7
8
9
10// templetes 目录下的所有页面,智能通过controller跳转,这个需要模板引擎的支持
public class IndexController {
public String index(Model model){
model.addAttribute("msg","hello springboot");
return "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:onclick="'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"> 1.all:删除包含标签和所有的孩子。2.body:不包含标记删除,但删除其所有的孩子。3.tag:包含标记的删除,但不删除它的孩子。4.all-but-first:删除所有包含标签的孩子,除了第一个。5.none:什么也不做。这个值是有用的动态评估。 |
th:attr | 设置标签属性,多个属性可以用逗号分隔 | 比如th:attr="src=@{/image/aa.jpg},title=#{logo}" ,此标签不太优雅,一般用的比较少。 |
示例
1、赋值、字符串拼接
1 | <p th:text="${collect.description}">description</p> |
字符串拼接还有另外一种简洁的写法
1 | <span th:text="|Welcome to our application, ${user.name}!|"> |
2、条件判断 If/Unless
Thymeleaf中使用th:if和th:unless属性进行条件判断,下面的例子中,<a>
标签只有在th:if
中条件成立时才显示:
1 | <a th:if="${myself=='yes'}" > </i> </a> |
th:unless
于 th:if
恰好相反,只有表达式中的条件不成立,才会显示其内容。
也可以使用 (if) ? (then) : (else)
这种语法来判断显示的内容
3、for 循环
1 | <tr th:each="collect,iterStat : ${collects}"> |
iterStat称作状态变量,属性有:
- index:当前迭代对象的 index(从0开始计算)
- count: 当前迭代对象的 index(从1开始计算)
- size:被迭代对象的大小
- current:当前迭代变量
- even/odd:布尔值,当前循环是否是偶数/奇数(从0开始计算)
- first:布尔值,当前循环是否是第一个
- last:布尔值,当前循环是否是最后一个
4、URL
URL 在 Web 应用模板中占据着十分重要的地位,需要特别注意的是 Thymeleaf 对于 URL 的处理是通过语法 @{...}
来处理的。
如果需要 Thymeleaf 对 URL 进行渲染,那么务必使用 th:href
,th:src
等属性,下面是一个例子
1 | <!-- Will produce 'http://localhost:8080/standard/unread' (plus rewriting) --> |
设置背景
1 | <div th:style="'background:url(' + @{/<path-to-image>} + ');'"></div> |
根据属性值改变背景
1 | <div class="media-object resource-card-image" th:style="'background:url(' + @{(${collect.webLogo}=='' ? 'img/favicon.png' : ${collect.webLogo})} + ')'" ></div> |
几点说明:
- 上例中 URL 最后的
(orderId=${o.id})
表示将括号内的内容作为 URL 参数处理,该语法避免使用字符串拼接,大大提高了可读性 @{...}
表达式中可以通过{orderId}
访问 Context 中的 orderId 变量@{/order}
是 Context 相关的相对路径,在渲染时会自动添加上当前 Web 应用的 Context 名字,假设 context 名字为 app,那么结果应该是/app/order
5、内联 js
内联文本:[[…]] 内联文本的表示方式,使用时,必须先用th:inline="text/javascript/none"
激活,th:inline
可以在父级标签内使用,甚至作为 body 的标签。内联文本尽管比th:text
的代码少,不利于原型显示。
1 | <script th:inline="javascript"> |
js 附加代码:
1 | /*[+ |
js 移除代码:
1 | /*[- */ |
6、内嵌变量
为了模板更加易用,Thymeleaf 还提供了一系列 Utility 对象(内置于 Context 中),可以通过 # 直接访问:
- dates : java.util.Date的功能方法类。
- calendars : 类似#dates,面向java.util.Calendar
- numbers : 格式化数字的功能方法类
- strings : 字符串对象的功能类,contains,startWiths,prepending/appending等等。
- objects: 对objects的功能类操作。
- bools: 对布尔值求值的功能方法。
- arrays:对数组的功能类方法。
- lists: 对lists功能类方法
- sets
- maps
…
下面用一段代码来举例一些常用的方法:
dates
1 | /* |
strings
1 | /* |