Web静态资源映射规则

SpringBoot中,SpringMVC的web配置都在 WebMvcAutoConfiguration 这个配置类里面;

我们可以去看看 WebMvcAutoConfigurationAdapter 中有很多配置方法;

有一个方法:addResourceHandlers 添加资源处理

@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
	super.addResourceHandlers(registry);
	// 如果自己配置了位置,就会禁用默认资源处理
	if (!this.resourceProperties.isAddMappings()) {
		logger.debug("Default resource handling disabled");
		return;
	}
	ServletContext servletContext = getServletContext();
    // webjars 配置
	addResourceHandler(registry, "/webjars/**", "classpath:/META-INF/resources/webjars/");
	
    // 静态资源配置
	addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) -> {
		registration.addResourceLocations(this.resourceProperties.getStaticLocations());
		if (servletContext != null) {
			registration.addResourceLocations(new ServletContextResource(servletContext, SERVLET_LOCATION));
		}
	});
}

什么是webjars 呢?

Webjars本质就是以jar包的方式引入我们的静态资源 , 我们以前要导入一个静态资源文件,直接导入即可。

使用SpringBoot需要使用Webjars,我们可以去搜索一下:

网站:https://www.webjars.org

jquery blockUI版本_java


要使用jQuery,我们只要要引入jQuery对应版本的pom依赖即可!

<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>jquery</artifactId>
    <version>3.6.0</version>
</dependency>

导入完毕,查看webjars目录结构,并访问Jquery.js文件!

jquery blockUI版本_jquery blockUI版本_02


访问:只要是静态资源,SpringBoot就会去对应的路径寻找资源,我们这里访问:http://localhost:8080/webjars/jquery/3.6.0/jquery.js

jquery blockUI版本_java_03

第二种静态资源映射规则

那我们项目中要是使用自己的静态资源该怎么导入呢?我们看下一行代码;

addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) -> {
				registration.addResourceLocations(this.resourceProperties.getStaticLocations());
				if (servletContext != null) {
					registration.addResourceLocations(new ServletContextResource(servletContext, SERVLET_LOCATION));
				}
			});

这一行代码看起开很复杂,但是我们可以看到喝上一行代码是一样的,那我们就参照上一行代码来看

上一行代码第二个参数是 "/webjars/* " ,也就是路径,那这一行也一样,应该是一个路径,我们点进去看

public String getStaticPathPattern() {
		return this.staticPathPattern;
	}

再点 staticPathPattern 进去

private String staticPathPattern = "/**";

终于看到了我们想要的

上一行代码是指我们在http://localhost:8080/webjars 后面的所有路径

这一个代码就是我们在http://localhost:8080 后面的所有路径都可以识别到

再看看下一个参数,有点复杂,暂时看不懂。问题不大,点 getStaticLocations() 进去,这个获得地址还是看的懂的

public String[] getStaticLocations() {
		return this.staticLocations;
	}

再点进去

jquery blockUI版本_java_04


看到了我们想要的东西了吧,四个路径,应该都认识

jquery blockUI版本_spring_05


我们可以在resources根目录下新建对应的文件夹,都可以存放我们的静态文件;

比如我们访问 http://localhost:8080/1.js , 他就会去这些文件夹中寻找对应的静态资源文件;

在这里是个文件是有优先级的说法的,resource>statics>public

自定义静态资源路径(不推荐)

我们也可以自己通过配置文件来指定一下,哪些文件夹是需要我们放静态资源文件的,在application.properties中配置;

一旦自己定义了静态文件夹的路径,原来的自动配置就都会失效了!

定制首页

静态资源文件夹说完后,我们继续向下看源码!可以看到一个欢迎页的映射,就是我们的首页!

@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));
	welcomePageHandlerMapping.setCorsConfigurations(getCorsConfigurations());
	return welcomePageHandlerMapping;
}

看到了 this.mvcProperties.getStaticPathPattern(),点进去可以看到又是全路径识别

又看到了 getWelcomePage(),点进去

jquery blockUI版本_静态资源_06

网页图标

jquery blockUI版本_jar_07


我做不出来,好像是版本太高,就没做了