-
th:fragment
即代码片段,用于另外的引用
<head th:fragment="common_header(title,links)">
<title th:replace="${title}">The awesome application</title>
<!-- Common styles and scripts -->
<link rel="stylesheet" type="text/css" media="all" th:href="@{/css/awesomeapp.css}">
<link rel="shortcut icon" th:href="@{/images/favicon.ico}">
<script type="text/javascript" th:src="@{/sh/scripts/codebase.js}"></script>
<!--/* Per-page placeholder for additional links */-->
<th:block th:replace="${links}" />
</head>
具体的使用方法如下:
_header.html:
<header class="navbar-wrapper" th:fragment="public_header">
...
</header>
_index.html:
<head th:replace = "_meta :: public_meta"></head>
和th:include
比较这个是插入标签内部的内容
- 这个用于当name没有值时,就输出no user authenticated
span th:text="${user.name} ?: _">no user authenticated</span>
- 在spring boot 中Thymeleaf一定要依赖下面这个包,不要依赖官网上的包(如果依赖你需要去配置),不然会得到404错误,因为映射不了
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
- themeleaf类似java中如下循环:
int pageSize = 10;
for(int i = 0; i < pageSize; i++){
}
实现如下
th:each="i : ${#numbers.sequence(0,pageSize)}"
- js取thymeleaf模板的值