自定义 .css 文件不适用于 Thymeleaf 和 Spring MVC

Mac*_*iaz 2 css spring spring-mvc thymeleaf bootstrap-4

所以,我花了像最后一个小时试图解决它,但我只是无法获得自定义 .css 文件与Spring 5 中的html 文件链接。我正在使用 Thymeleaf、BootstrapjQuery进行前端工作。我正在使用 Intellij 社区版。

我的文件层次结构如下所示:

java
    project_files
resources
    static
        css
          main.css
    templates
        main.html
Run Code Online (Sandbox Code Playgroud)

我有一行main.html

<link rel="stylesheet" type="text/css" th:href="@{/css/main.css}" href="../../css/main.css"/>
Run Code Online (Sandbox Code Playgroud)

这应该将我的样式与 .html 文件链接起来。有什么问题?

jua*_*umn 5

尝试稍微更改您的文件夹结构,在资源下创建一个名为static的文件夹,并在 static下创建一个名为css的文件夹并将 css 文件放在那里,例如:

 resources
????  static
    ????  css
        ???? main.css
Run Code Online (Sandbox Code Playgroud)

然后您应该可以使用以下方法访问它:

 <link rel="stylesheet" type="text/css" th:href="@{/css/main.css}"/>
Run Code Online (Sandbox Code Playgroud)