带有Thymeleaf的SpringBoot - 未找到css [英] SpringBoot with Thymeleaf - css not found
问题描述
首先要说的是,我现在一直在寻找解决方案,现在我非常绝望。
First to say is that I've been searching for a solution for a while now and I'm quite desperate now.
我无法获取css文件可以在Spring Boot运行时从html页面访问。
I cannot get the css file to be accessible from html page when run by Spring Boot.
html.file
html.file
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head lang="en">
<title th:text='#{Title}'>AntiIntruder</title>
<meta charset="UTF-8" />
<link rel="stylesheet" type="text/css" media="all" href="../assets/css/style.css" th:href="@{/css/style.css}" />
</head>
<body>
...
Application.java
Application.java
@SpringBootApplication // adds @Configuration, @EnableAutoConfiguration, @ComponentScan
@EnableWebMvc
public class Application extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/assets/**").addResourceLocations("classpath:/assets/*");
}
}
文件夹结构:
我已经尝试将 css
文件夹放入 static
文件夹和/或删除addResourcesHandlers,引用css by相对路径和其他一些事情。似乎没有解决这个问题。
如果你试图解决这个问题,请告诉我,但是没有找到解决办法,所以我知道,我不会被忽视。
I've tried putting the css
folder into a static
folder and/or removing the addResourcesHandlers, referencing to the css by relative path and some other things. Nothing seems to resolve this.
Please, let me know also if you tried to solve this but did not find a solution, so that I know, that I'm not ignored.
推荐答案
问题是 Application.java
文件中的 @EnableWebMvc
注释。一旦我删除了那个,css就开始在 localhost:8080 / css / style.css
上可用,但是没有应用。到目前为止,我还没有找到 @EnableWebMvc
导致问题的原因。
The problem was the @EnableWebMvc
annotation in the Application.java
file. As soon as I removed that one, the css started to be available at localhost:8080/css/style.css
but was not applied. So far I haven't found the reason why the @EnableWebMvc
was causing the problem.
然后我删除了控制器映射到我为了显示自定义错误页面而实现的 / **
。
Then I removed a controller mapped to /**
that I had implemented in order to display custom error page.
@RequestMapping("/**")
public String notFound() {
return "errors/404";
}
在删除这个之后,我的css正在运行。 =)
After removing also this one, I've got my css working. =)
这篇关于带有Thymeleaf的SpringBoot - 未找到css的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!