1、XML方式配置
<mvc:resources mapping="/resources/**"
location="/public, classpath:/static/"
cache-period="31556926" />
2、Java Config方式配置,实现WebMvcConfigurer接口,覆盖addResourceHandlers方法
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**")
.addResourceLocations("/public", "classpath:/static/")
.setCachePeriod(31556926);
}
}
3、Spring Boot Priperties文件配置
#对应xml方式的mapping属性
spring.mvc.static-path-pattern=/resources/**
#缓存时间
spring.resources.cache.period=3600
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/