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

标签 html css spring-mvc thymeleaf

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

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

这是在 Tomcat 中部署的 WAR 的样子: enter image description here

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。

新的目录结构: enter image description here

还有我的 index.html 中的新代码片段

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

关于html - 使用 th :href 在 spring mvc 中引用带有 thymeleaf 的 .css 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34048576/

相关文章:

javascript - 网络和物理单位

html - 如何使我的网站可针对不同设备进行调整,并且不比我打开的窗口更宽?

java - 最佳选择 - Spring XML 解析以读取某些元素

javascript - 导航时 jquery 选项卡的问题

php - 如何通过 window.print() 打印页面的某些特定部分

css - 2 列布局,列高与页脚一致

java - 无法将类转换为 java.lang.reflect.ParameterizedType

java - 如何从 spring mvc Controller 调用外部 api

html - 外部定义的 SVG 标记和过滤器在 Chrome 中不起作用

html - <div> 垂直对齐