使用 th:href 在 spring mvc 中引用带有 thymeleaf 的 .css 文件

Reference a .css file with thymeleaf in spring mvc using th:href

我的 index.html thymeleaf 页面即将出现,但找不到我在该页面上引用的 CSS。

<link href="../css/bootstrap.min.css" rel="stylesheet" th:href="@{/css/bootstrap.min.css}" type="text/css"/>

这是 WAR 在 Tomcat 中部署的样子:

index.html 在模板文件夹中。

但我在 Chrome 控制台中收到以下错误:

GET http://localhost:8080/pocApp/css/bootstrap.min.css 404 (Not Found)

如果我可以正常访问 index.html 页面,为什么 thymeleaf servlet 找不到该文件?我尝试将整个 css 目录粘贴到目录结构中的不同位置,例如 WEB-INF 和 WEB-INF/classes 中,但没有成功。

这个网站很快就成了我的橡皮鸭,因为这是我自己回答的第二个问题。

在我的 Spring 4 Java 配置中我有这个:

public class SpringWebConfig extends WebMvcConfigurerAdapter {

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/resources/**")
            .addResourceLocations("/resources/");
}

所以我只需要重组我的目录并更改样式表 URL。

新目录结构:

以及来自我的 index.html

的新代码片段
<link href="../resources/css/bootstrap.min.css" rel="stylesheet" th:href="@{/resources/css/bootstrap.min.css}" type="text/css"/>