spring-boot - Spring Boot、Thymeleaf 和 CSS

标签 spring-boot thymeleaf

这真的很傻,但我无法让它工作。

在我的 spring boot mvc 应用程序中,假设有 5 个 thymeleaf 模板,其中一个是 error.html。

当对任何无效路由发出请求时,error.html 会派上用场。

当嵌套无效路由(如 2 层或更多层)然后 css 不适用时,就会出现此问题。

例如:

http://localhost:3000/application/index- valid route and css is applied
http://localhost:3000/application/success- valid route and css is applied
http://localhost:3000/application/failure- valid route and css is applied
http://localhost:3000/application/invalidroute- route does not exist but css is applied
http://localhost:3000/application/invalidroute/something - route does not exist and css is also not applied

我的CSS位于static文件夹下的css文件夹

所有的 thymeleaf 模板都在同一层级并且通过 css 访问

 <link rel="stylesheet" href="css/main.css"/>

在控制台中看到错误

GET http://localhost:3000/application/invalidroute/css/main.css net::ERR_ABORTED 404

最佳答案

代替

<link rel="stylesheet" href="css/main.css"/>

使用这个:

<link rel="stylesheet" th:href="@{/css/main.css}" />

并确保这是你的文件占位符

src/main/resource/static/css - 用于 CSS 文件

src/main/resource/templates - 用于 HTML 模板文件

关于spring-boot - Spring Boot、Thymeleaf 和 CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61553420/

相关文章:

jquery - HTML 动态元素有不同的大小

spring - 将 bean 字段传递给 :field in Thymeleaf fragment

java - Spring Batch 并行处理多次执行一个步骤

java - Spring Boot - Firebase 管理 SDK - 通知

java - 如何在散列之前验证最小、最大密码长度?

Spring Boot - 多个模板位置

Thymeleaf - 在模板中配置邮件主题

java - 如何使用 thymeleaf 构建包含请求参数的 URL 路径?

tomcat - 我可以为 Spring Boot 的嵌入式 tomcat 启用 tomcat 管理器应用程序吗?

java - 如何在Spring boot 2.0中实现CrudRepository的自定义方法?