如何使用 Thymeleaf 在 Micronaut 中加载 css 文件?

How to load css file in Micronaut using Thymeleaf?

如何使用 Thymeleaf 在 Micronaut 中加载 CSS 文件?

这是我的index.html内容:

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Demo</title>
    <link th:href="@{public/style.css}" type="text/css" rel="stylesheet" />
</head>
<body></body>
</html>

这里是application.yml:

router:
  static:
    resources:
      default:
        enabled: true
        mapping: /**
        paths: 'classpath:public'

图片注释:

映射mapping: /** 会将其挂载到根目录下。如果您希望它在 public 下可用,您可能需要向该映射添加 /public 前缀

你的配置有两个错误:

  1. 正如 Graeme Rocher 所提到的,mapping: /** 会将 public 文件夹绑定到您的应用程序的根路径,即文件将在 <BASE_URL>/style.css 处可用,但您希望它位于 HTML 文件中的 <BASE_URL>/public/style.css

  2. 您的配置定义不正确。即它应该以 micronaut 开头而不是 router

以下配置为您修复了问题:

micronaut:
  router:
    static-resources:
      default:
        enabled: true
        mapping: "/public/**"
        paths: "classpath:public"