上一篇博文介紹瞭如何使用Freemaker引擎搭建web項目,這一篇咱們則看一下另一個常見的頁面渲染引擎Thymeleaf如何搭建一個web項目css
推薦結合Freemaker博文一塊兒查看,效果更佳 190816-SpringBoot系列教程web篇之Freemaker環境搭建html
Thymeleaf 是現代化服務器端的Java模板引擎,不一樣與JSP和FreeMarker,Thymeleaf的語法更加接近HTML,關於它的使用說明,能夠參考官方文檔 www.thymeleaf.org/documentati…前端
首先咱們是須要一個springboot項目,基本的pom結構大都類似java
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from update -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
<java.version>1.8</java.version>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</pluginManagement>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
複製代碼
在這個項目中,咱們主要須要引入兩個依賴包,一個web,一個thymeleafgit
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
複製代碼
一般咱們直接使用默認的thymeleaf參數配置便可,下面給出幾個經常使用的配置github
spring:
thymeleaf:
mode: HTML
encoding: UTF-8
servlet:
content-type: text/html
cache: false
複製代碼
thymeleaf的參數,主要對應的是org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties
web
搭建一個web項目和咱們以前的純後端項目有點不同,前端資源放在什麼地方,依賴文件怎麼處理都是有講究的,下面是一個常規的項目結構spring
如上圖,前端資源文件默認放在resources目錄下,下面有兩個目錄後端
templates
:存放模板文件,能夠理解爲咱們編寫的html,注意這個文件名不能有問題static
: 存放靜態資源文件,如js,css,image等咱們這裏提供了三個接口,主要是爲了演示三種不一樣的數據綁定方式(和Freemaker這篇博文基本同樣)springboot
@Controller
public class IndexController {
@GetMapping(path = {"", "/", "/index"})
public ModelAndView index() {
Map<String, Object> data = new HashMap<>(2);
data.put("name", "YiHui Thymeleaf");
data.put("now", LocalDateTime.now().toString());
return new ModelAndView("index", data);
}
/** * 通常不建議直接使用jdk的String.split來分割字符串,內部實現是根據正則來處理的,雖然更強大,但在簡單的場景下,性能開銷更大 */
private static String[] contents =
("綠蟻浮觴香泛泛,黃花共薦芳辰。\n清霜天宇淨無塵。\n登高宜有賦,拈筆戲成文。\n可奈園林搖落盡,悲秋意與誰論。\n眼中相識幾番新。\n龍山高會處,落帽定何人。").split("\n");
private static Random random = new Random();
@GetMapping(path = "show1")
public String showOne(Model model) {
model.addAttribute("title", "臨江仙");
model.addAttribute("content", contents[random.nextInt(6)]);
return "show1";
}
@GetMapping(path = "show2")
public String showTow(Map<String, Object> data) {
data.put("name", "Show2---->");
data.put("now", LocalDateTime.now().toString());
return "show2";
}
}
複製代碼
上面的三種case中
ModelAndView
時,傳入viewName和數據三個接口,對應的三個html文件,以下
index.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="SpringBoot thymeleaf"/>
<meta name="author" content="YiHui"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>YiHui's SpringBoot Demo</title>
<link rel="stylesheet" href="index.css"/>
</head>
<body>
<div>
<div class="title">hello world!</div>
<br/>
<div class="content" th:text="'歡迎訪問' + ${name}">默認的內容</div>
<br/>
<div class="sign" th:text="'當前時間' + ${now}">默認的簽名</div>
<br/>
<a href="show1">傳參2測試</a>
<a href="show2">傳參3測試</a>
</div>
</body>
</html>
複製代碼
show1.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="SpringBoot thymeleaf"/>
<meta name="author" content="YiHui"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>YiHui's SpringBoot Demo</title>
<link rel="stylesheet" href="index.css"/>
</head>
<body>
<div>
<div class="title" th:text="${title}">標題!</div>
<div class="content" th:text="${content}">內容</div>
</div>
</body>
</html>
複製代碼
show2.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="SpringBoot thymeleaf"/>
<meta name="author" content="YiHui"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>YiHui's SpringBoot Demo</title>
<link rel="stylesheet" href="index.css"/>
</head>
<body>
<div>
<div class="title" th:text="${name}">標題!</div>
<div class="content" th:text="${now}">內容</div>
</div>
</body>
</html>
複製代碼
在上面的模板文件中,須要注意引用css樣式文件,路徑前面並無static,咱們對應的css文件
index.css
.title {
color: #c00;
font-weight: normal;
font-size: 2em;
}
.content {
color: darkblue;
font-size: 1.2em;
}
.sign {
color: lightgray;
font-size: 0.8em;
font-style: italic;
}
複製代碼
啓動項目後,能夠看到三個頁面的切換,模板中的數據根據後端的返回替換,特別是主頁的時間,每次刷新都會隨之改變
項目地址: github.com/liuyueyi/sp…
盡信書則不如,以上內容,純屬一家之言,因我的能力有限,不免有疏漏和錯誤之處,如發現bug或者有更好的建議,歡迎批評指正,不吝感激
下面一灰灰的我的博客,記錄全部學習和工做中的博文,歡迎你們前去逛逛