学习资料参考
静态资源映射
webjars
如何导入
对一些统一的资源框架,如jquery,bootstrap等可以通过webjars的官网查找依赖导入pom.xml来获取资源
webjars官网
以jquery为例
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.4.1</version>
</dependency>
如何使用
Ctrl+Shift+R
从源码上看可以知道
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
} else {
Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
if (!registry.hasMappingForPattern("/webjars/**")) {
this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
...
}
凡是/webjars/**的访问都会通过根路径下的classpath:/META-INF/resources/webjars/
进行处理,这个在上一个目录结构图中也比较清晰
如果要访问 静态资源可以直接通过这种映射进行访问(PS:我severPort=8082)
同理 在正常使用的时候只需要在需要的时候按照以上静态映射导入静态资源即可达到预期的目的
模糊静态资源映射规则
还是刚才那一小段得源码出处
...
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
if (!registry.hasMappingForPattern(staticPathPattern)) {
this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(WebMvcAutoConfiguration.getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
...
这里的 new String[]{staticPathPattern}
点进去可以最终看到
this.staticPathPattern = "/**";
而this.resourceProperties.getStaticLocations())
点进去可以最终看到private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
这意味着任何无处理的请求都将到
{“classpath:/META-INF/resources/”, “classpath:/resources/”, “classpath:/static/”, “classpath:/public/”}
这几个路径中去查找
撇开类目录下的/META-INF/不谈,譬如 在如下三个文件夹中任意一个文件 都能被直接得访问
欢迎页的静态映射
在mvc的自动配置类WebMvcAutoConfiguration.class
中
关于欢迎页源码是这样的
@Bean
public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext) {
WelcomePageHandlerMapping welcomePageHandlerMapping = new WelcomePageHandlerMapping(new TemplateAvailabilityProviders(applicationContext), applicationContext, this.getWelcomePage(), this.mvcProperties.getStaticPathPattern());
welcomePageHandlerMapping.setInterceptors(this.getInterceptors());
return welcomePageHandlerMapping;
}
WelcomePageHandlerMapping
这个类的构造函数是这样的
WelcomePageHandlerMapping(TemplateAvailabilityProviders templateAvailabilityProviders, ApplicationContext applicationContext, Optional<Resource> welcomePage, String staticPathPattern) {
if (welcomePage.isPresent() && "/**".equals(staticPathPattern)) {
logger.info("Adding welcome page: " + welcomePage.get());
this.setRootViewName("forward:index.html");
} else if (this.welcomeTemplateExists(templateAvailabilityProviders, applicationContext)) {
logger.info("Adding welcome page template: index");
this.setRootViewName("index");
}
private Optional<Resource> getWelcomePage() {
String[] locations = WebMvcAutoConfiguration.getResourceLocations(this.resourceProperties.getStaticLocations());
return Arrays.stream(locations).map(this::getIndexHtml).filter(this::isReadable).findFirst();
}
..
private String[] staticLocations;
..
public ResourceProperties() {
this.staticLocations = CLASSPATH_RESOURCE_LOCATIONS;
..
}
意味着"\**"
无处理的时候会在String[] CLASSPATH_RESOURCE_LOCATIONS
也就是 {"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"}
中寻找index.html作为欢迎页
静态映射的图标
@Bean
public SimpleUrlHandlerMapping faviconHandlerMapping() {
SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
mapping.setOrder(-2147483647);
mapping.setUrlMap(Collections.singletonMap("**/favicon.ico", this.faviconRequestHandler()));
return mapping;
}
表明任何favicon.ico都将被识别并被应用于icon