thymeleaf:在一個頁面中引入其它的頁面


這個在jsp中很容易實現,但是springBoot不推薦使用jsp,建議使用thymeleaf,下面是在thymeleaf中引入界面的方法

1.修改配置文件

spring:
  mvc:
    static-path-pattern: /static/**
  thymeleaf:
    prefix:
      classpath: /templates/

在springBoot中,靜態資源(如js,css,圖片,靜態html)都放在static當中,動態資源(如html)放在templates中。

這是我的目錄結構

 

 

 

 現在我要在index.html中引入main.html

 

2.修改main.html

 

 

用<div th:fragment="main"></div>把整個頁面包裹起來,包裹的內容就是你要在index.html引入的部分

 

3.修改index.html,因為springBoot自動把static當作當前目錄,所以要用../templates/shop/main而不是/templates/shop/main。

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <div th:replace="../templates/shop/main :: main"></div>
</body>
</html>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM