首先创建一个配置类,配置类不继承WebMvcConfigureSupport类,需要实现WebMvcConfigurer接口
@Configuration
public class ResourceConfigure implements WebMvcConfigurer {
public void ARH(ResourceHandlerRegistry registry){
registry.addResourceHandler("/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX+"/templates/");
registry.addResourceHandler("/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX + "/static/");
}
}
addResourceHandler的路径表示资源路径开头,如果为/static/开头,资源路径就为/static/***
在HTML页面里面,css资源路径应被@{}包裹,script文件用src=“”
spring boot的配置文件(yml)相关配置:
mvc:
view:
prefix: WEB-INF/page/
suffix: .jsp
thymeleaf:
prefix: classpath:/templates/
check-template-location: true
cache: false
suffix: .html
encoding: UTF-8
mode: HTML5
servlet:
content-type: text/html