springboot学习之十六(thymeleaf模板引擎公共代码片段)

与f / 2023-07-25 / 原文

 

 -----

自己的例子

common.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>common</title>
</head>
<body>

<div th:fragment="fragment-name" id="fragment-id">
    <span>公共页面片段</span>
</div>

<div  th:fragment="header(title)">
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title th:text="${title}"></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">
    <style>
        body{
            background: rgba(255, 0, 0, 0.2);
        }
    </style>
</div>

<div  th:fragment="footer_script">
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.6/dist/umd/popper.min.js" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.min.js"crossorigin="anonymous"></script>
</div>

</body>

</html>

使用

<!DOCTYPE html>
<html  lang="en" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
<head>
    <th:block th:include="/comm/common :: header('登录页面')" />
</head>
...

 

 http://c.biancheng.net/spring_boot/thymeleaf.html