1. 程式人生 > >spring boot Thymeleaf 頁面包含和頁面熱部署

spring boot Thymeleaf 頁面包含和頁面熱部署

1、Thymeleaf 頁面包含其它頁面

在開發web應用的過程中,經常需要引用到其它的頁面。舉例來說,我有兩個頁面一個頁面叫index.html,另外一個頁面叫session.html。現在index.html需要將session.html引入。在spring boot專案的templates資料夾下面新建這兩個檔案。
session.html中的程式碼如下:

<div th:fragment="copy" xmlns:th="http://www.w3.org/1999/xhtml">
    &copy; 2014 The Good Thymes Virtual Grocery
</div
>
index.html的程式碼如下
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <title>主頁面</title>
</head>
<body>
    <h4>5說點什麼吧!</h4>
    <div th:replace="session::copy"></div>
</body
>
</html>

使用標籤 th:replace替換html中的片段,其中::前面的session是檔名稱,copy是session.html中的div的id

2、Thymeleaf 熱部署

在開發的過程中,頁面經常修改而希望不需要重新啟動,使用Thymeleaf模板需要在application.properties檔案中,加上一行:

spring.thymeleaf.cache=false

之後修改完html頁面之後,在IDEA中按下ctrl + shift + F9 鍵重新編譯,或者在編輯檔案中,按右鍵選擇重新編譯檔案。再重新整理瀏覽器即可更新頁面。