Thymeleaf 模板引擎實用指南【更新中】

Thymeleaf 模板引擎實用指南【更新中】


thymeleaf一款是功能強大的模板引擎,且與Spring Boot 無縫集成,是代替Spring MVC + JSP的首選

配置

Maven配置

如果使用的是Spring Boot 2.1.x版本,那麼不需要額外配置
pom.xml

<!--默認與Spring Boot 保持一致-->
		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

如果使用的是Spring Boot 1.15.x,那就會有一個小坑,這與Thymeleaf的解析器有關(在解析單標籤時可能會提示沒有結束標籤),所以需要在pom.xml中如下配置一些配置,具體如下:

	...
	<properties>
		...
		<!--這裏-->
        <thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
        <thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version>
        ...
    </properties>
    ...

命名空間

想使用thymeleaf模板引擎進行解析的HTML頁面需要在頁面中引入thymeleaf的命名空間,才能使用thymeleaf特有的標籤
命名空間:http://www.thymeleaf.org
示例:

<!DOCTYPE html>
<!--這裏↓↓↓↓↓↓↓↓-->
<html xmlns:th="http://www.thymeleaf.org" lang="zh">
<head>
    <meta charset="UTF-8" />
    <title>示例</title>
</head>
<body>
發佈了10 篇原創文章 · 獲贊 0 · 訪問量 230
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章