如何在 thymleaf 中添加额外的 css 和 js 文件

问题描述 投票:0回答:0

我为我的页面使用布局,布局包括包含可重用代码的片段。但这里有一个问题,我的类别 html 需要一个特定的 css 和 js 文件。我尝试添加标题标签但没有发生任何事情。

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:fragment="head_html">
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
              integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N"
              crossorigin="anonymous">

        <link rel="preconnect" href="https://fonts.googleapis.com">
        <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
        <link href="https://fonts.googleapis.com/css2?family=Lora:wght@400;500;600&display=swap" rel="stylesheet">
        <link rel="stylesheet" type="text/css" href="/css/coffee.css" th:href="@{css/coffee.css}">
        <script src="https://kit.fontawesome.com/bbe981fbfc.js" crossorigin="anonymous"></script>
    </th:block>
</head>
<body>
  <!-- other fragments-->
</body>

我的布局

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" th:fragment="layout (content)">
<head>
    <div th:replace="~{fragment/user_fragment :: head_html}"></div>
</head>
<body>
<div th:replace="~{fragment/user_fragment :: navigation}"></div>

<div th:insert="${content}"></div>

<div th:replace="~{fragment/user_fragment :: footer}"></div>


<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.slim.min.js"
        integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj"
        crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
        integrity="sha384-Fy6S3B9q64WdZWQUiU+q4/2Lc9npb8tCaSX9FK7E8HnRr0Jz8D6OP9dO5Vg3Q9ct"
        crossorigin="anonymous"></script>
<script src="/javascript/script.js" th:src="@{/javascript/script.js}"></script>
</body>
</html>

还有我的类别

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      th:replace="~{layout/user_layout :: layout(~{::content})}">
<head>
    <link rel="stylesheet" href="/css/what.css" th:href="@{css/what.css}">
    <script src="/javascript/what.js" th:src="@{javascript/what.js}"></script>
</head>
<body>
    <!-- code -->
</body>

我确实添加了类别标题,但它没有加载任何文件。我该怎么办?

javascript html css thymeleaf
© www.soinside.com 2019 - 2024. All rights reserved.