按照 《精通 SpringMVC4》里边的代码敲,他用的是 SpringBoot:1.2.5.RELEASE
,我用的是 SpringBoot:2.1.4.RELEASE
,然后 layout
模板不起作用。
原因是 SpringBoot2.x
中的 thymeleaf
把 thymeleaf-layout-dialect
给单独拆出来了。
- 所需依赖
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
// 2.x 和 1.x 就差在这儿了
implementation 'nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect:2.3.0'
- layout 页面
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html" charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Default title</title>
<link href="/webjars/materializecss/0.96.0/css/materialize.css" type="text/css" rel="stylesheet"
media="screen,projection"/>
</head>
<body>
<!-- 塞代码的地方 -->
<section layout:fragment="content">
<p>Page content goes here</p>
</section>
<script src="/webjars/jquery/2.1.4/jquery.js"></script>
<script src="/webjars/materializecss/0.96.0/js/materialize.js"></script>
</body>
</html>
- 使用 layout 页面
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorator="layout/default"
lang="en">
<head>
<meta charset="UTF-8">
<title>Hello thymeleaf</title>
</head>
<body>
<div layout:fragment="content">
<span>Hello, world</span>
</div>
</body>
</html>
https://lixi.fun/2019/05/04/springboot2-x-thymeleaf/