首先要说的是,我一直在寻找解决方案有一段时间了,现在我非常绝望。
当由 Spring Boot 运行时,我无法从 html 页面访问 css 文件。
html文件
<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>
...
应用.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。似乎没有什么可以解决这个问题。如果您尝试解决此问题但没有找到解决方案,请也让我知道,以便我知道,我没有被忽略。
原文由 Bato 发布,翻译遵循 CC BY-SA 4.0 许可协议
问题是
Application.java
文件中的@EnableWebMvc
注释。我一删除那个,css 就开始在localhost:8080/css/style.css
可用,但没有应用。到目前为止,我还没有找到@EnableWebMvc
导致问题的原因。然后我删除了一个映射到
/**
的控制器,我为了显示自定义错误页面而实现了它。在删除了这个之后,我的 CSS 开始工作了。 =)