天天看點

Spring Boot 引入 Thymeleaf 及入門1. 模闆引擎2. Spring Boot 使用 Thymeleaf3. 引入 Thymeleaf4. 渲染流程規則5. 背景控制層6. 前台頁面手動渲染Thymeleaf源碼解析

文章目錄

  • 1. 模闆引擎
  • 2. Spring Boot 使用 Thymeleaf
  • 3. 引入 Thymeleaf
  • 4. 渲染流程規則
  • 5. 背景控制層
  • 6. 前台頁面
  • 手動渲染Thymeleaf
  • 源碼解析

以前開發 web 項目時,隻需将靜态的 "html” 頁面字尾名修改為“jsp”,然後在檔案中加入jsp頁面辨別即可做jsp開發。

1. 模闆引擎

1、市面上主流的 Java 模闆引擎有:JSP、Velocity、Freemarker、Thymeleaf

2、JSP 本質也是模闆引擎,Spring Boot 預設是不支援jsp的,官方推薦使用 “Thymeleaf”模闆引擎

3、模闆引擎原理圖如下,模闆引擎的作用都是将模闆(頁面)和資料進行整合然後輸出顯示,差別在于不同的模闆使用不同的文法,如 JSP 的JSTL表達式,以及JSP 自己的表達式和文法,同理 Thymeleaf 也有自己的文法

Spring Boot 引入 Thymeleaf 及入門1. 模闆引擎2. Spring Boot 使用 Thymeleaf3. 引入 Thymeleaf4. 渲染流程規則5. 背景控制層6. 前台頁面手動渲染Thymeleaf源碼解析

2. Spring Boot 使用 Thymeleaf

1、Thymeleaf 官網:https://www.thymeleaf.org/

2、Thymeleaf 在Github 的首頁:https://github.com/thymeleaf/thymeleaf

3. 引入 Thymeleaf

1、使用 Thymeleaf 同樣隻需要在 pom.xml 引入此子產品即可

2、官方文檔:“https://docs.spring.io/spring-boot/docs/2.0.3.RELEASE/reference/htmlsingle/#using-boot-starter”

<!-- 引入thymeleaf模闆引擎-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
           

4. 渲染流程規則

1、使用非常簡單,可以找到它的自動配置檔案即可檢視它的使用規則

Spring Boot 引入 Thymeleaf 及入門1. 模闆引擎2. Spring Boot 使用 Thymeleaf3. 引入 Thymeleaf4. 渲染流程規則5. 背景控制層6. 前台頁面手動渲染Thymeleaf源碼解析

2、其中預設規則如下:

  • 預設字首:DEFAULT_PREFIX = “classpath:/templates/”
  • 預設字尾:DEFAULT_SUFFIX = “.html”

這完全類似 Spring MVC 的映射規則,如果想修改這些配置隻需要在全局配置檔案中覆寫修改即可。

3、是以預設隻要把 HTML 頁面放在 classpath:/templates/ 下,thymeleaf 就能自動渲染, classpath:/templates/ 目錄以外的 html 檔案是無法使用 Thymeleaf 引擎的。

4、當然自己可以在全局配置檔案中覆寫修改這些規則,修改的選項可以從 “ThymeleafProperties” 檔案中找到,也可以參 考官方文檔配置:

#THYMELEAF (ThymeleafAutoConfiguration)
spring.thymeleaf.prefix=classpath:/templates/ # Prefix that gets prepended to view names when building a URL.
spring.thymeleaf.servlet.content-type=text/html # Content-Type value written to HTTP responses.
spring.thymeleaf.suffix=.html # Suffix that gets appended to view names when building a URL.
           

5. 背景控制層

/**
 * @RestController :表示本類所有的方法都是直接做内容傳回到浏覽器
 * @Controller :控制層
 */
@Controller
public class HelloController {

    @RequestMapping("/home")
    public String home(Map<String, Object> paramMap) {
        /** 預設Map的内容會放大請求域中,頁面可以直接取值*/
        paramMap.put("name", "James");
        /** 會自動跳轉到預設的 classpath:/templates/success.html 頁面*/
        return "home";
    }

}
           

注意不要使用

@RestController

作為控制層,

@RestController

預設将方法傳回值直接傳回給浏覽器,不會做視圖映射。

在控制層中傳回

邏輯視圖名+資料

,邏輯視圖名為

home

,意思是需要在

resources/templates

目錄下提供一個名為

home.html

的 Thymeleaf 模闆檔案。

6. 前台頁面

<!DOCTYPE html>
<!--作用是讓IDEA編輯器有Thymeleaf文法提示,不寫不影響運作-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
	<!--取值文法之一-->
    <p th:text="${name}"></p>
</body>
</html>
           

浏覽器通路:

http://localhost:8080/home

手動渲染Thymeleaf

上面都是自動渲染 Thymeleaf 模闆後傳回給浏覽器,我們也可以手動渲染 Thymeleaf 模闆,這個一般在郵件發送時候有用,例如在 resources/templates 目錄下建立一個郵件模闆,檔案名:

mail.html

,如下:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<p>hello 歡迎 <span th:text="${username}"></span>加入 XXX 集團,您的入職資訊如下:</p>
<table border="1">
    <tr>
        <td>職位</td>
        <td th:text="${position}"></td>
    </tr>
    <tr>
        <td>薪水</td>
        <td th:text="${salary}"></td>
    </tr>
</table>
<img src="http://www.javaboy.org/images/sb/javaboy.jpg" alt="">
</body>
</html>
           

現在需要将這個 HTML 模闆渲染成一個 String 字元串。

@SpringBootTest
@RunWith(SpringRunner.class)
public class DemoTests {

    @Autowired
    TemplateEngine templateEngine;

    @Test
    public void test(){
        Context context = new Context();
        context.setVariable("username", "James");
        context.setVariable("position", "Java工程師");
        context.setVariable("salary", 99999);
        String mail = templateEngine.process("mail", context);
        System.out.println(mail);
    }

}
           
  • 注入一個 TemplateEngine 對象,這個對象就是在 Thymeleaf 的自動化配置類中配置的(即當我們引入 Thymeleaf 的依賴之後,這個執行個體就有了)。
  • 構造一個 Context 對象用來存放變量。
  • 調用 process 方法進行渲染,該方法的傳回值就是渲染後的 HTML 字元串。

源碼解析

Spring Boot 為 Thymeleaf 提供了自動化配置類

org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration

Spring Boot 引入 Thymeleaf 及入門1. 模闆引擎2. Spring Boot 使用 Thymeleaf3. 引入 Thymeleaf4. 渲染流程規則5. 背景控制層6. 前台頁面手動渲染Thymeleaf源碼解析

自動化配置類中,使用 ThymeleafProperties 作為配置類,然後 @ConditionalOnClass 注解表示當目前系統中存在 TemplateMode 和 SpringTemplateEngine 類時,目前的自動化配置類才會生效,即隻要項目中引入了 Thymeleaf 相關的依賴,這個配置就會生效。

檢視下 ThymeleafProperties 配置類。

Spring Boot 引入 Thymeleaf 及入門1. 模闆引擎2. Spring Boot 使用 Thymeleaf3. 引入 Thymeleaf4. 渲染流程規則5. 背景控制層6. 前台頁面手動渲染Thymeleaf源碼解析

前三個 static 變量定義了預設的編碼格式、視圖解析器的字首、字尾等。

可以看出來,Thymeleaf 模闆的預設位置在 resources/templates 目錄下,預設的字尾是 html 。

預設的配置幾乎不需要做任何更改就可以直接使用了。

如果有特殊需求,則可以在 application.properties 中配置以 spring.thymeleaf 開頭的屬性即可。

參考:

Spring Boot 引入 Thymeleaf 及入門