Spring Boot、Thymeleaf 和 CSS [英] Spring Boot, Thymeleaf and CSS

查看:70
本文介绍了Spring Boot、Thymeleaf 和 CSS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这真的很愚蠢,但我无法让它工作.

This is really silly but I am not able to get it working.

在我的 spring boot mvc 应用程序中,我假设有 5 个百里香叶模板,其中之一是 error.html.

In my spring boot mvc app, I have let's say 5 thyme leaf templates one of which is error.html.

error.html 在对任何无效路由发出请求时派上用场.

error.html comes handy when a request is made for any invalid routes.

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

The issue happens when the invalid routes are nested (like 2 or more level) then the css dont apply.

例如:

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 文件夹

My CSS is located in css folder under the static folder

所有的 thymeleaf 模板都在同一层级,通过

All the thymeleaf templates are at the same level and access the css by

 <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}" />

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

and make sure this is your file placeholders

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

src/main/resource/static/css - for CSS files

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

src/main/resource/templates - for HTML templates files

这篇关于Spring Boot、Thymeleaf 和 CSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆