SpringBoot核心技术-核心功能-Web开发

文章详细阐述了SpringBoot中静态资源的访问方式,包括默认路径和自定义路径,以及如何配置欢迎页和favicon。同时,解释了SpringBoot的自动配置类WebMVCAutoConfiguration如何处理静态资源,并提供了禁用静态资源映射的配置。此外,还介绍了欢迎页的处理规则和判断机制。
摘要由CSDN通过智能技术生成

简单功能访问

1.静态资源访问

  • 静态资源目录
    类路径下:called/static(or /public or /resources or /META-INF/resources)
    访问:当前项目根路径/+静态资源名

原理:静态映射/**,
请求进来,先去找Controller看能不能处理,不能处理的所有请求去访问静态资源

  • 静态资源访问前缀
    默认无前缀
spring:
	mvc:
		static-path-pattern: /res/**

当前项目+static-path-pattern+静态资源文件夹下找

2. 欢迎页支持

  • 静态资源路径下 index.html
  • 可以配置静态资源路径
  • 不能配置静态资源访问前缀,否则导致index.html不能被默认访问
spring:
#  mvc:
#    static-path-pattern: /res/**
#  resources:
    static-locations: [classpath:/public/]

3.自定义 favicon
4.静态资源配置原理

  • SpringBoot启动默认加载 xxxAutoConfiguration类(自动配置类)
  • SpringMVC功能的自动配置类 WebMVCAutoConfiguration
  • 给容器中配了什么

    @Configuration(proxyBeanMethods = false)
    @Import({WebMvcAutoConfiguration.EnableWebMvcConfiguration.class})
    @EnableConfigurationProperties({WebMvcProperties.class,ResourceProperties.class})
    @Order(0)
  • 配置文件的相关属性和xxx进行了绑定,WebMvcProperties==spring.mvc、ResourceProperties==spring.resouces

配置类只要一个有参构造器

//有参构造器所有参数的值都会从容器中确定
//ResourceProperties resourcesProperties 获取和spring.resouces绑定的所有的值的对象 
//WebMvcProperties mvcProperties 获取和spring.mvc的所有的值的对象
//ListableBeanFactory beanFactory Spring的beanFactory
//HttpMessageConverters 找到所有的HttpMessageConverters
//ResourceHandlerRegistrationCustomizer 找到资源处理器的自定义器
//DispatcherServletPath
//ServletRegistrationBean 给应用注册 Servlet、Filter....

public WebMvcAutoConfigurationAdapter(ResourceProperties resourceProperties, WebMvcProperties mvcProperties, ListableBeanFactory beanFactory, ObjectProvider<HttpMessageConverters> messageConvertersProvider, ObjectProvider<WebMvcAutoConfiguration.ResourceHandlerRegistrationCustomizer> resourceHandlerRegistrationCustomizerProvider, ObjectProvider<DispatcherServletPath> dispatcherServletPath, ObjectProvider<ServletRegistrationBean<?>> servletRegistrations) {
            this.resourceProperties = resourceProperties;
            this.mvcProperties = mvcProperties;
            this.beanFactory = beanFactory;
            this.messageConvertersProvider = messageConvertersProvider;
            this.resourceHandlerRegistrationCustomizer = (WebMvcAutoConfiguration.ResourceHandlerRegistrationCustomizer)resourceHandlerRegistrationCustomizerProvider.getIfAvailable();
            this.dispatcherServletPath = dispatcherServletPath;
            this.servletRegistrations = servletRegistrations;
        }

资源处理的默认规则

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();

//Webjars规则
                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));
                }

                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));
                }

            }
        }

1.禁用所有静态资源

spring:
#  mvc:
#    static-path-pattern: /res/**
#  resources:
    static-locations: [classpath:/public/]
    resources:
      add-mappings: false

2.欢迎页处理规则

@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;
        }



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");
        }

    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值