SpringBoot-Web开发静态资源处理-06

1.Web开发研究

使用SpringBoot的步骤

  1. 创建一个springboot的的应用,选择我们需要的模块,springboot就会默认将我们需要的模块自动配置好
  2. 手动在配置文件中配置部分配置,项目就可以运行
    必须了解自动配置
  • 向容器中自动配置的组件xxxAutoconfiguration
  • 自动配置类,封装配置文件的内容:xxxproperties

2. 静态资源处理

静态资源映射规则

  1. 需要怎么引入前端资源呢,项目中有很多的静态资源,css js 等,springboot会怎么处理
  2. 如果是web应用,我们的main下面会有一个webapp,我们以前都是将页面放在这里但是现在的pom打包的方式是jar包,SpringBoot对于静态资源放置的位置是有规定的!

1. 静态资源映射规则

SpringBoot中,SpringMVC的web配置都在WebMvcAutoConfiguration这个配置类里面;
可以看到里面有很多的配置方法;
其中有一个方法:WebMvcAutoConfigurationAdapter中的addResourceHandelers添加资源处理


   public void addResourceHandlers(ResourceHandlerRegistry registry) {
            if (!this.resourceProperties.isAddMappings()) {
		//禁用默认的资源处理
                logger.debug("Default resource handling disabled");
            } else {
		//webjars配置
                this.addResourceHandler(registry, "/webjars/**", "classpath:/META-INF/resources/webjars/");
                this.addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) -> {
                    registration.addResourceLocations(this.resourceProperties.getStaticLocations());
                    if (this.servletContext != null) {
                        ServletContextResource resource = new ServletContextResource(this.servletContext, "/");
                        registration.addResourceLocations(new Resource[]{resource});
                    }

                });
            }
        }

读源码可知:/webjars/** ,都需要去classpath:/META-INF/redsources/webjars/找对应的资源

webjars是什么

Webjars本质就是以jar包的方式引入我们的静态资源 , 我们以前要导入一个静态资源文件,直接导入pom.xml即可。
使用SpringBoot需要使用Webjars,我们可以去搜索一下:
网站:https://www.webjars.org
导入之后就可以在/META-INF/redsources/webjars/看到jquery文件,其中会有版本,版本进入会有jquery.js

2. 第二中静态资源映射规则

去找staticPathPattern发现第二种映射规则:/** ,访问当前的项目任意资源,会自动去找resourceProperties这个类,如下分析

// 进入方法
public String[] getStaticLocations() {
    return this.staticLocations;
}
// 找到对应的值
private String[] staticLocations = CLASSPATH_RESOURCE_LOCATIONS;
// 找到路径
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
    "classpath:/META-INF/resources/",
    "classpath:/resources/",
    "classpath:/static/",
    "classpath:/public/"
};

resourceProperties可以设置和我们的静态资源有关参数;这里面指向了他会去寻找资源的文件夹,即上述数组内容.
得出结论以下的四个目录存放的静态资源可被识别:

"classpath:/META-INF/resources/"
"classpath:/resources/"
"classpath:/static/"
"classpath:/public/"

我们可以在resources根目录新建对应的文件夹,都可以存放我们的静态资源文件
例如http://localhost:8080/1.js ,他就会去这些文件夹中寻找对应的静态资源文件;

3. 首页处理

继续向下看源码可知:有个欢迎页的映射

  @Bean
        public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext, FormattingConversionService mvcConversionService, ResourceUrlProvider mvcResourceUrlProvider) {
            WelcomePageHandlerMapping welcomePageHandlerMapping = new WelcomePageHandlerMapping(new TemplateAvailabilityProviders(applicationContext), applicationContext, this.getWelcomePage(), this.mvcProperties.getStaticPathPattern());
            welcomePageHandlerMapping.setInterceptors(this.getInterceptors(mvcConversionService, mvcResourceUrlProvider));
            welcomePageHandlerMapping.setCorsConfigurations(this.getCorsConfigurations());
            return welcomePageHandlerMapping;
        }

进如getWelcomePage可以看到

 private Resource getWelcomePage() {
            String[] var1 = this.resourceProperties.getStaticLocations();
            int var2 = var1.length;

            for(int var3 = 0; var3 < var2; ++var3) {
                String location = var1[var3];
                Resource indexHtml = this.getIndexHtml(location);
                if (indexHtml != null) {
                    return indexHtml;
                }
            }
  1. 欢迎页,静态资源文件夹下的所有index.html页面,会被/** 映射
  2. 例如我们访问http://localhost:8080/ , 就会找到静态资源文件夹下的index.html
posted @ 2021-09-24 16:10  1_f  阅读(26)  评论(0)    收藏  举报