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

Pra*_*ngh 3 thymeleaf micronaut

如何使用 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>
Run Code Online (Sandbox Code Playgroud)

这是application.yml

router:
  static:
    resources:
      default:
        enabled: true
        mapping: /**
        paths: 'classpath:public'
Run Code Online (Sandbox Code Playgroud)

图片说明:

资源目录结构

Adi*_*a T 6

你的配置有两个错误:

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

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

以下配置为您解决了问题:

micronaut:
  router:
    static-resources:
      default:
        enabled: true
        mapping: "/public/**"
        paths: "classpath:public"
Run Code Online (Sandbox Code Playgroud)