一、新建SpringBoot項目springboot-thymeleaf,引入以下pom依賴
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.thinkingcao</groupId>
<artifactId>springboot-thymeleaf</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-thymeleaf</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
二、靜態資源訪問
在我們開發Web應用的時候,需要引用大量的js、css、圖片等靜態資源。
三、默認配置
Spring Boot默認提供靜態資源目錄位置需置於classpath下,目錄名需符合如下規則:
- /static
- /public
- /resources
- /META-INF/resources
舉例:我們可以在src/main/resources/
目錄下創建static
,在該位置放置一個圖片文件。啓動程序後,嘗試訪問http://localhost:8080/D.jpg
。如能顯示圖片,配置成功。
四、渲染Web頁面
在之前的示例中,我們都是通過@RestController來處理請求,所以返回的內容爲json對象。那麼如果需要渲染html頁面的時候,要如何實現呢?
五、模板引擎
在動態HTML實現上Spring Boot依然可以完美勝任,並且提供了多種模板引擎的默認配置支持,所以在推薦的模板引擎下,我們可以很快的上手開發動態網站。
Spring Boot提供了默認配置的模板引擎主要有以下幾種:
- Thymeleaf
- FreeMarker
- Velocity
- Groovy
- Mustache
Spring Boot建議使用這些模板引擎,避免使用JSP,並且SpringBoot官方默認的模板引擎是Thymeleaf,若一定要使用JSP作爲模板引擎,將無法實現Spring Boot的多種特性,具體可見後文:支持JSP的配置
當你使用上述模板引擎中的任何一個,它們默認的模板配置路徑爲:src/main/resources/templates
。當然也可以修改這個路徑,具體如何修改,可在後續各模板引擎的配置屬性中查詢並修改。
Thymeleaf
Thymeleaf是一個XML/XHTML/HTML5模板引擎,可用於Web與非Web環境中的應用開發。它是一個開源的Java庫,基於Apache License 2.0許可,由Daniel Fernández創建,該作者還是Java加密庫Jasypt的作者。
Thymeleaf提供了一個用於整合Spring MVC的可選模塊,在應用開發中,你可以使用Thymeleaf來完全代替JSP或其他模板引擎,如Velocity、FreeMarker等。Thymeleaf的主要目標在於提供一種可被瀏覽器正確顯示的、格式良好的模板創建方式,因此也可以用作靜態建模。你可以使用它創建經過驗證的XML與HTML模板。相對於編寫邏輯或代碼,開發者只需將標籤屬性添加到模板中即可。接下來,這些標籤屬性就會在DOM(文檔對象模型)上執行預先制定好的邏輯。
網上示例模板:
<table>
<thead>
<tr>
<th th:text="#{msgs.headers.name}">Name</td>
<th th:text="#{msgs.headers.price}">Price</td>
</tr>
</thead>
<tbody>
<tr th:each="prod : ${allProducts}">
<td th:text="${prod.name}">Oranges</td>
<td th:text="${#numbers.formatDecimal(prod.price,1,2)}">0.99</td>
</tr>
</tbody>
</table>
可以看到Thymeleaf主要以屬性的方式加入到html標籤中,瀏覽器在解析html時,當檢查到沒有的屬性時候會忽略,所以Thymeleaf的模板可以通過瀏覽器直接打開展現,這樣非常有利於前後端的分離。
在Spring Boot中使用Thymeleaf,只需要引入下面依賴,並在默認的模板路徑src/main/resources/templates
下編寫模板文件即可完成。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
六、 在完成配置之後,舉一個簡單的例子
1. 新建編輯控制層代碼HelloController,在request添加了name屬性,
HelloController.java
package com.thinkingcao.springbootthymeleaf.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
@Controller
public class HelloController {
@RequestMapping("/hello")
public String hello(HttpServletRequest request, @RequestParam(value = "name", defaultValue = "默認值springboot-thymeleaf") String name) {
request.setAttribute("name", name);
return "index";
}
}
2.新建編輯模板文件,在resources文件夾下的templates目錄,用於存放HTML等模板文件,在這新增hello.html,添加如下代碼
index.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--<p th:text="'hello, ' + ${name} + '!'"/>-->
<h1 th:text="${name}">Hello World</h1>
</body>
</html>
切記:使用Thymeleaf模板引擎時,必須在html文件上方添加該行代碼使用支持Thymeleaf。
<html lang="en" xmlns:th="http://www.thymeleaf.org">
如上頁面,直接打開html頁面展現Hello World,但是啓動程序後,
訪問http://localhost:8080/hello,則是展示Controller中name的值:默認值springboot-thymeleaf,
如果訪問http://localhost:8080/hello?name=SpringBoot整合thymeleaf測試 ;那麼則顯示的是name的值,做到了不破壞HTML自身內容的數據邏輯分離。
3.啓動項目,訪問http://localhost:8080/hello,看到如下顯示證明SpringBoot整合Thymeleaf成功。
Thymeleaf的默認參數配置
如有需要修改默認配置的時候,只需複製下面要修改的屬性到application.properties
中,並修改成需要的值,如修改模板文件的擴展名,修改默認的模板路徑等。
# 是否使用緩存,開發階段最填false,方便使用ctrl+shift+F9 進行重新編譯,無需重啓服務
spring.thymeleaf.cache=false
# 檢查該模板是否存在
spring.thymeleaf.check-template-location=true
# 模板中內容的類型
spring.thymeleaf.content-type=text/html
# 啓動 MVC 對Thymeleaf 視圖的解析
spring.thymeleaf.enabled=true
# 模板的字符集
spring.thymeleaf.encoding=UTF-8
# 從解析中排除的視圖名稱的逗號分隔列表,沒有的話就空咯
spring.thymeleaf.excluded-view-names=
# 使用的是什麼類型模板
spring.thymeleaf.mode=HTML5
# 在構建URL時可以預覽查看名稱的前綴。就是路徑在哪
spring.thymeleaf.prefix=classpath:/templates/
# 在構建URL時附加到視圖名稱的後綴。就是我們用rest風格,不同加文件後綴名。自己加上去
spring.thymeleaf.suffix=.html
七、項目完整結構圖
八、支持JSP的配置
九、參考資料