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/");
// 解决swagger无法访问。
registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/", "/static", "/public");
// 解决swagger的js文件无法访问。
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}

  • WebMvcAutoConfigurationaddResourceHandlers方法的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/"
  • 可以在resources根目录下新建对应的文件夹,都可以存放我们的静态文件。
  • 比如访问 http://localhost:8080/1.js ,则会在这些文件夹中寻找对应的静态资源文件。

自定义静态资源路径

  • 通过配置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
  1. 关闭SpringBoot默认图标。
1
spring.mvc.favicon.enabled=false
  1. 放一个图标在静态资源目录下,文件名默认为favicon.ico
  2. 测试:清除浏览器缓存并刷新网页。

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!