二 :springboot整合Thymeleaf
常用restful传值方式
@GetMapping("/reply/{id}") 使用管道符拼接
public String reply(@PathVariable("id") int id)
<a th:href="@{|/reply/${id}|}">查看回复</a>
<a th:href="@{/reply/}"+${id}>查看回复</a>
@GetMapping("/reply")
public String reply(int id)
<a th:href="@{/reply(id=${item.id})}">查看回复</a>
POM
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
2.1.6
</dependency>
<properties>
<thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
<!-- 布局功能的支持程序 thymeleaf3主程序 layout2以上版本 -->
<!-- thymeleaf2 layout1-->
<thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version>
</properties>
Thymeleaf语法
1.方言(Standard Dialect)就跟内置对象还差不多
也可以定制自己的标签在Spring容器中
<input type="text" name="userName" value="James Carrot" th:value="${user.name}" />
2.html引用
<html xmlns:th="http://www.thymeleaf.org">
data-均允许使用,而该符号仅在HTML模式中允许。
<!DOCTYPE html>
<html>
<head>
<title>Good Thymes Virtual Grocery</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" media="all"
href="../../css/gtvg.css" data-th-href="@{/css/gtvg.css}" />
</head>
<body>
创建一个properties 从中 有 Home.welcome=“打撒” 获得这个值
<p data-th-text="#{home.welcome}">Welcome to our grocery store!</p>
</body>
</html>
2.1不同会话取值
${x}将返回x存储在Thymeleaf上下文中或作为请求属性的变量。
${param.x}将返回一个称为的请求参数x(可能是多值)。
${session.x}将返回名为的会话属性x。
${application.x}将返回名为的Servlet上下文属性x。
2.2 表达式
简单表达式:
变量表达式: ${...}
选择变量表达式: *{...}
消息表达: #{...}
链接URL表达式: @{...}
片段表达式: ~{...}
文字
文本文字:'one text','Another one!',...
号码文字:0,34,3.0,12.3,...
布尔文字:true,false
空文字: null
文字标记:one,sometext,main,...
文字操作:
字符串串联: +
文字替换: |The name is ${name}|
算术运算:
二元运算符:+,-,*,/,%
减号(一元运算符): -
布尔运算:
二元运算符:and,or
布尔否定(一元运算符): !,not
比较和平等:
比较:>,<,>=,<=(gt,lt,ge,le)
等号运算符:==,!=(eq,ne)
条件运算符:
如果-则: (if) ? (then)
如果-则-否则: (if) ? (then) : (else)
默认: (value) ?: (defaultvalue)
2.3表达式基本对象
#ctx:上下文对象。
#vars: 上下文变量。
#locale:上下文语言环境。
#request:(仅在Web上下文中)HttpServletRequest对象。
#response:(仅在Web上下文中)HttpServletResponse对象。
#session:(仅在Web上下文中)HttpSession对象。
#servletContext:(仅在Web上下文中)ServletContext对象。
示例:
<span th:text="${#locale.country}">US</span>
2.4 * 选择表达式
*{} 代表对象的属性
<div th:object="${session.user}">
<p>Name: <span th:text="*{firstName}">Sebastian</span>.</p>
<p>Surname: <span th:text="*{lastName}">Pepper</span>.</p>
<p>Nationality: <span th:text="*{nationality}">Saturn</span>.</p>
</div>
2.5 url
- 绝对路径
- 相对路径
<!-- Will produce 'http://localhost:8080/gtvg/order/details?orderId=3' (plus rewriting) -->
<a href="details.html"
th:href="@{http://localhost:8080/gtvg/order/details(orderId=${o.id})}">view</a>
<!-- Will produce '/gtvg/order/details?orderId=3' (plus rewriting) -->
<a href="details.html" th:href="@{/order/details(orderId=${o.id})}">view</a>
<!-- Will produce '/gtvg/order/3/details' (plus rewriting) -->
<a href="details.html" th:href="@{/order/{orderId}/details(orderId=${o.id})}">view</a>
<a th:href="@{${url}(orderId=${o.id})}">view</a>
<a th:href="@{'/details/'+${user.login}(orderId=${o.id})}">view</a>
2.6 文字替代
<span th:text="|Welcome to our application, ${user.name}!|">
等效于
<span th:text="'Welcome to our application, ' + ${user.name} + '!'">
<span th:text="${onevar} + ' ' + |${twovar}, ${threevar}|">
2.7迭代(遍历)
<tr th:each="prod : ${prods}">
<td th:text="${prod.name}">Onions</td>
<td th:text="${prod.price}">2.41</td>
<td th:text="${prod.inStock}? #{true} : #{false}">yes</td>
</tr>
3.模板布局
用途: 页面复用
- 页脚
- 页眉
创建一个footer.html页面
定义变量
- th:fragment="copy"
引用变量代码
~{templatename::selector}
~{templatename::fragmentname}
页面/变量名
- th:insert="~{footer :: copy}"
示例:~{templatename::fragmentname}
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<body>
<div th:fragment="copy">
© 2011 The Good Thymes Virtual Grocery
</div>
</body>
</html>
页面引用
<body>
...
<div th:insert="~{footer :: copy}"></div>
</body>
示例二:
~{templatename::selector}
<div id="copy-section">
© 2011 The Good Thymes Virtual Grocery
</div>
<body>
...
<div th:insert="~{footer :: #copy-section}"></div>
</body>
在本页定义使用:
定义变量
<div th:fragment="frag (onevar,twovar)">
<p th:text="${onevar} + ' - ' + ${twovar}">...</p>
</div>
从后台获取 ${value1}
<div th:replace="::frag (${value1},${value2})">...</div>
<div th:replace="::frag (onevar=${value1},twovar=${value2})">...</div>
3.1 布局继承
<!DOCTYPE html>
<html th:fragment="layout (title, content)" xmlns:th="http://www.thymeleaf.org">
<head>
<title th:replace="${title}">Layout Title</title>
</head>
<body>
<h1>Layout H1</h1>
<div th:replace="${content}">
<p>Layout content</p>
</div>
<footer>
Layout footer
</footer>
</body>
</html>
layoutFile 表示页面
layout(~{::title}, ~{::section})
- ::title 表示这个页面的标签title
- ::section 表示这个页面的标签section
<!DOCTYPE html>
<html th:replace="~{layoutFile :: layout(~{::title}, ~{::section})}">
<head>
<title>Page Title</title>
</head>
<body>
<section>
<p>Page content</p>
<div>Included on page</div>
</section>
</body>
</html>
4 内联
4.1 输出表达式
[[${session.user.name}]]
[(${msg})] 不转义 html元素保留
4.2 javascript 内联
可以在script标签使用 thymeleaf语法
<script th:inline="javascript">
...
var username = [[${session.user.name}]];
...
</script>
4.3 thymeleaf 内嵌表达式
- dates : java.util.Date的功能方法类。
- calendars : 类似#dates,面向java.util.Calendar
- numbers : 格式化数字的功能方法类
- strings : 字符串对象的功能类,
- contains,startWiths,prepending/appending等等。
- objects: 对objects的功能类操作。
- bools: 对布尔值求值的功能方法。
- arrays:对数组的功能类方法。
- lists: 对lists功能类方法
- sets
- maps
…
[[${#dates.format(item.createdate,'yyyy-MM-dd')}]]
最后
可以跟Freemark一样自定义标签