spring.mvc.static-path-pattern=/static/**
//只有静态资源的访问路径为/static/**时,才会处理请求
spring.resources.static-locations=classpath:static/
#高版本freemarker 视图文件的后缀默认是ftlh 不改请求不到前端页面
spring.freemarker.suffix=.ftl
spring.mvc.static-path-pattern
用于阐述HTTP请求地址,默认值/**
spring.resources.static-locations
则用于描述静态资源的存放位置。
这是一个列表性的配置,用于说明静态资源文件的查找路径,查找文件是会依赖于配置的先后顺序依次进行,默认的官方配置如下:
#默认配置
spring.resources.static-locations =
classpath:/static,
classpath:/public,
classpath:/resources,
classpath:/META-INF/resources
@Deprecated
WebMvcConfigurerAdapter
WebMvcConfigurer
WebMvcConfigurationSupport
这里需要注意的是,使用WebMvcConfigurationSupport会使WebMvcAutoConfiguration的自动配置不生效,因为WebMvcAutoConfiguration里面有个@ConditionalOnMissingBean(WebMvcConfigurationSupport.class),表现为默认的一些映射路径无法访问,如果想保留默认的配置,那就使用implements WebMvcConfigurer的方式来配置 原文链接:https://blog.csdn.net/w57685321/article/details/92797551
@Configuration
public class WebConfigConfigurer extends WebMvcConfigurationSupport {
// 配置Cache-Control
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**")
.addResourceLocations("classpath:/static/")
.setCacheControl(CacheControl.maxAge(604800, TimeUnit.SECONDS));
}
}
@Configuration
public class WebConfigConfigurer implements WebMvcConfigurer {
//配置Last-Modified缓存
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**")
.addResourceLocations("classpath:/static/")
.setCacheControl(
CacheControl.maxAge(0, TimeUnit.SECONDS)
.cachePublic());
}
//配置ETag缓存
@Bean
public FilterRegistrationBean filterRegistrationBean () {
ShallowEtagHeaderFilter eTagFilter = new ShallowEtagHeaderFilter();
//设置为weakETag,默认为false
// eTagFilter.setWriteWeakETag(true);
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(eTagFilter);
registration.addUrlPatterns("/static/*");
return registration;
}
}
@EnableWebMvc 会使默认MVC的默认配置失效
@Configuration
@EnableWebMvc
public class WebConfigure implements WebMvcConfigurer
因此,通常我们采用实现WebMvcConfigurer接口,然后让Spring Boot采用默认的配置。 https://cloud.tencent.com/developer/article/1774555
配置缓存:https://blog.csdn.net/weixin_38405253/article/details/118714639