1. 程式人生 > >SpringBoot web開發--模板引擎(Thymeleaf)學習

SpringBoot web開發--模板引擎(Thymeleaf)學習

模板引擎

對於web開發的同學我們應該知道很多常用的模板引擎,例如jsp、Velocity、Freemarker、Thymeleaf等。以前我們可以使用jsp在jsp中遍歷後端傳遞的值或者判斷等等。那現在我們如果使用springboot開發了該怎麼辦呢?
當然是springboot給我們提供了強大的模板引擎Thymeleaf,語法更簡單,功能更強大;

1、引入thymeleaf;

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId
>
spring-boot-starter-thymeleaf</artifactId> </dependency> <!--切換thymeleaf版本--> <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>

這是ThymeleafAutoConfiguration自動配置類,我們可以通過ThymeleafProperties類看看一些預設配置

@ConfigurationProperties(prefix = "spring.thymeleaf")
public class ThymeleafProperties {

	private static final Charset DEFAULT_ENCODING = StandardCharsets.
UTF_8; public static final String DEFAULT_PREFIX = "classpath:/templates/"; public static final String DEFAULT_SUFFIX = ".html"; /** * Whether to check that the template exists before rendering it. */ private boolean checkTemplate = true;

裡面有很多預設的配置,這裡只列舉一部分,有興趣的同學可以自己檢視,從上面的預設配置我們可以看出來,springboot回去類路徑下的templates去檢視以.html結尾的檔案,什麼意思呢?我們可以舉個例子來學習:

  • 編寫一個successcontroller
@RestController
public class SuccessController {

    @RequestMapping("/success")
    public String success(){
        return "success";
    }
}

然後我們在resouces目錄下建立templates目錄,在該目錄下建立一個success.html,

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<h1>
    success
</h1>

</body>
</html>

啟動專案傳送請求http://localhost:8080/success檢視結果。

是不是很方便,只要我們把HTML頁面放在classpath:/templates/,thymeleaf就能自動渲染;

什麼是Thymeleaf

官方網站

Thymeleaf是適用於Web和獨立環境的現代伺服器端Java模板引擎。

Thymeleaf的主要目標是提供一種優雅且高度可維護的模板建立方式。 為實現這一目標,它以自然模板的概念為基礎,將其邏輯注入模板檔案,其方式不會影響模板被用作設計原型。 這改善了設計溝通,縮小了設計和開發團隊之間的差距。

藉助Spring Framework的模組,與您喜歡的工具的大量整合,以及插入您自己的功能的能力,Thymeleaf是現代HTML5 JVM Web開發的理想選擇 。

用Thymeleaf編寫的HTML模板看起來和HTML一樣工作,讓在您的應用程式中執行的實際模板繼續作為有用的設計工件。

Thymeleaf是一個適用於Web和獨立環境的現代伺服器端Java模板引擎,能夠處理HTML,XML,JavaScript,CSS甚至純文字。

特點

Out-of-the-box,Thymeleaf允許您處理六種模板,每種模板稱為模板模式:

HTML
XML
文字
JAVASCRIPT
CSS
生的
有兩種標記模板模式(HTML和XML),三種文字模板模式(TEXT,JAVASCRIPT和CSS)和無操作模板模式(RAW)。

語法規則

thymeleaf官方使用手冊
這裡只做一些常用的介紹,大家平時遇到問題可以去官方網站去檢視相關的語法規則。

使用:

1、匯入thymeleaf的名稱空間

<html lang="en" xmlns:th="http://www.thymeleaf.org">

匯入thymeleaf的名稱空間可以方便我們使用,不匯入也不會報錯。

我們通過一個例項進行學習:

@Controller
public class HelloController {

  //傳遞資料在頁面顯示
  @RequestMapping(method = RequestMethod.GET,path = "/success")
  public String success(Map<String,Object> map){
      map.put("hello","<h1>你好</h1>");
      map.put("users", Arrays.asList("zhangsan","lisi","wangwu"));
      return "success";
  }

}

然後我們在resouces目錄下建立templates目錄,在該目錄下建立一個success.html,

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
<!--text 與utext 的區別在於能不能轉義字元-->
<div th:id="${hello}" th:class="${hello}" th:text="${hello}"> 歡迎新使用者 </div>
<!--行內寫法-->
<div th:utext="${hello}"> [[${hello}]]歡迎新使用者 </div>


<!--遍歷-->
<h1 th:text="${user}" th:each="user:${users}"></h1>

<h1>
  <span th:each="user:${users}">[[${user}]] </span>
</h1>
</body>
</html>
  • 結果
<h1>你好</h1>
你好
zhangsan
lisi
wangwu

zhangsan lisi wangwu

小技巧

我們開發期間可能要對於頁面進行修改,那怎麼才能實時生效呢?

  1. 禁用模板引擎快取:spring.thymeleaf.cache=false
  2. 頁面修改完成後(IDEA)ctrl+f9:重新編譯。