无法在Spring5.0.6中通过addResourceHandler和thymeleaf导入静态资源
我有以下文件夹结构:无法在Spring5.0.6中通过addResourceHandler和thymeleaf导入静态资源,spring,spring-mvc,thymeleaf,Spring,Spring Mvc,Thymeleaf,我有以下文件夹结构:'resources'>'static'>'styles' “styles”文件夹中有文件style.css 还有这一类: @Configuration @EnableWebMvc public class ResourcesConfiguration implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry)
'resources'>'static'>'styles'
“styles”文件夹中有文件
style.css
还有这一类:
@Configuration
@EnableWebMvc
public class ResourcesConfiguration implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/styles/**")
.addResourceLocations("/styles/", "classpath:/static/");
}
}
在index.html
中,在head
部分有:
<link th:href="@{/styles/style.css}" rel="stylesheet" type="text/css" />
我没有关于静态资源的其他配置(如application.properties)
当我在浏览器中访问index.html
时,我得到:请求URL:
请求方法:获取
状态代码:404
addResourceLocations(字符串…位置)
将在不同的位置作为变量参数列表。但它的工作原理与相邻条目的前缀或后缀类似
因此,您应该稍微更改您的配置,如下所示,以使其正常工作
registry
.addResourceHandler("/styles/**")
.addResourceLocations("classpath:/static/styles/");