[英]Fix location path to .css with Thymeleaf (Spring Boot)
I'm working on a project with Spring Boot.我正在使用 Spring 启动项目。 I have a main.css file under /src/main/resources/static/css/
我在/src/main/resources/static/css/
下有一个 main.css 文件
I use this thymeleaf code to inject the.css file into a.html file from the /templates folder:我使用此 thymeleaf 代码将 .css 文件从 /templates 文件夹注入到 a.html 文件中:
<link rel="stylesheet" th:href="@{/css/main.css}" href="../static/css/main.css" />
I can open it respectively via http://localhost:1126/css/main.css
我可以分别通过http://localhost:1126/css/main.css
打开它
I use this.html as a detailed error page.我使用this.html作为详细的错误页面。 So if an URL doesn't exists, show this.html.因此,如果 URL 不存在,请显示 this.html。 If the URL is with "one depth" (for example localhost:1126/something
) it works fine (.html is shown and.css is loaded).如果 URL 具有“一个深度”(例如localhost:1126/something
),它工作正常(显示.html 并且加载了.ZC7A628CBA22E28EB17B5F5C6AE2A266)。
But if I use URLs with at least "two depths", or even with an "/" at the end (for example localhost:1126/something/
or localhost:1126/something/anything
) it doesn't work (.html is shown but.css IS NOT loaded).但是,如果我使用的 URL 至少有“两个深度”,甚至最后有一个“/”(例如localhost:1126/something/
或localhost:1126/something/anything
),它就不起作用(.html 是显示但未加载 ZC7A62 8CBA22E28EB17B5F5C6AE2A266AZ )。
The problem is that in the second case Spring try to find the.css file under localhost:1126/something/css/main.css
问题是在第二种情况下 Spring 尝试在localhost:1126/something/css/main.css
下找到 .css 文件
Things I've tried so far:到目前为止我尝试过的事情:
use th:href="@{/css/main.css}"
instead of th:href="@{css/main.css}"
使用th:href="@{/css/main.css}"
代替th:href="@{css/main.css}"
AND和
@SpringBootApplication
@EnableWebMvc
public class SpringBootProjectApplication implements WebMvcConfigurer {
public static void main(String[] args) {
SpringApplication.run(SpringBootProjectApplication.class, args);
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/css/**").addResourceLocations("classpath:/css/");
}
}
I have found these threads with no answers for my problem:我发现这些线程没有解决我的问题:
Thymeleaf, IntelliJ and Spring Boot not loading CSS files correctly Thymeleaf、IntelliJ 和 Spring 引导未正确加载 CSS 文件
Spring Boot, Thymeleaf and CSS Spring 引导、Thymeleaf 和 CSS
CSS file cannot be located thymeleaf Spring Boot CSS 文件无法定位 thymeleaf Spring 引导
Spring boot default BasicErrorController would directly look at src/main/resources/templates/ folder if you add your error html page there as "error.html" you can show that whenever error occurs Spring 启动默认 BasicErrorController 将直接查看 src/main/resources/templates/ 文件夹,如果您将错误 html 页面添加为“error.html”,您可以显示每当发生错误时
I faced the same issue when my URL had multiple "/".当我的 URL 有多个“/”时,我遇到了同样的问题。 But I did not make any special changes except for changing my css links as follows.但是我没有做任何特别的改变,除了改变我的 css 链接如下。
<link rel="stylesheet" th:href="@{~/css/main.css}">
And same with javascript files as well. javascript 文件也一样。
On a side note, I don't use @EnableWebMvc annotation.附带说明一下,我不使用 @EnableWebMvc 注释。 It kinda messes up with automatic SpringMVC configuration.它有点搞砸了自动 SpringMVC 配置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.