Spring MVC 静态资源处理
本文最后更新于:2024年9月8日 晚上
Spring MVC 静态资源处理
静态资源映射规则
addResourceHandlers
添加资源处理。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| @Configuration public class WebMvcConfig implements WebMvcConfigurer {
@Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/"); registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/", "/static", "/public"); registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/"); } }
|
- 在
WebMvcAutoConfiguration
的addResourceHandlers
方法的staticPathPattern
中发现第映射规则 /**
- 访问当前的项目任意资源,它会去找
resourceProperties
这个类,可以打开分析。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| @ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false) public class ResourceProperties { private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/" };
private String[] staticLocations = CLASSPATH_RESOURCE_LOCATIONS;
public String[] getStaticLocations() { return this.staticLocations; } }
|
ResourceProperties
可以设置和我们静态资源有关的参数,这里面指向了它会去寻找资源的文件夹,即上面数组的内容。
- 所以得出结论,以下四个目录存放的静态资源可以被识别。
1 2 3 4
| "classpath:/META-INF/resources/" "classpath:/resources/" "classpath:/static/" "classpath:/public/"
|
自定义静态资源路径
- 通过配置
application.properties
来指定存放静态资源文件的目录。
1
| spring.resources.static-locations=classpath:/coding/,classpath:/test/
|
- 注意:一旦自定义了静态文件夹的路径,原来的自动配置就都会失效。
webjars
- Webjars本质就是以jar包的方式引入我们的静态资源。
- 所有的
/webjars/**
,都需要去classpath:/META-INF/resources/webjars/
找对应的资源。
实例
- 要使用jQuery,只要引入jQuery对应版本的pom依赖即可。
1 2 3 4 5
| <dependency> <groupId>org.webjars</groupId> <artifactId>jquery</artifactId> <version>3.4.1</version> </dependency>
|
- 导入完毕,查看webjars目录结构。
- 只要是静态资源,SpringBoot就会去对应的路径寻找资源。

首页映射规则
WebMvcAutoConfiguration
类中关于首页映射规则的方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| @Bean public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext, FormattingConversionService mvcConversionService, ResourceUrlProvider mvcResourceUrlProvider) { WelcomePageHandlerMapping welcomePageHandlerMapping = new WelcomePageHandlerMapping( new TemplateAvailabilityProviders(applicationContext), applicationContext, getWelcomePage(), this.mvcProperties.getStaticPathPattern()); welcomePageHandlerMapping.setInterceptors(getInterceptors(mvcConversionService, mvcResourceUrlProvider)); return welcomePageHandlerMapping; }
private Optional<Resource> getWelcomePage() { String[] locations = getResourceLocations(this.resourceProperties.getStaticLocations());
return Arrays.stream(locations).map(this::getIndexHtml).filter(this::isReadable).findFirst(); }
private Resource getIndexHtml(String location) { return this.resourceLoader.getResource(location + "index.html"); }
|
配置网站图标
- 与其他静态资源一样,Spring Boot在配置的静态内容位置中查找
favicon.ico
- 如果存在这样的文件,它将自动用作应用程序的favicon
- 关闭SpringBoot默认图标。
1
| spring.mvc.favicon.enabled=false
|
- 放一个图标在静态资源目录下,文件名默认为
favicon.ico
- 测试:清除浏览器缓存并刷新网页。