【SpringBoot】welcome与favicon功能以及一些底层

182 阅读1分钟

本文已参与[新人创作礼]活动,一起开启掘金创作之路。 ​

 欢迎页配置

默认放在static文件夹下,index.html即为欢迎页

手动修改欢迎页的位置

在application.yaml文件中配置如下

spring:
  web:
    resources:
      static-locations: [classpath:/haha/]

​编辑

 ​编辑

 注意:可以配置静态资源路径,但不可以配置静态资源的访问前缀,否则导致index.html不能被默认访问

Favicon

将图标放到静态资源路径,默认是static,因为上面我们改了,所以是haha路径下,图标命名应为favicon.ico

​编辑

 ​编辑

注意:如果浏览器访问网站时网站还没有图标,后配置,但是浏览器没关,以后图标都不会出现,这是因为浏览器session的缘故,所以需要换一个浏览器尝试,同样也不可以配置静态资源路径前缀,否则会影响功能的实现。

底层分析

大多数配置都在这个Servlet文件夹里

​编辑

​编辑

 给容器中配了什么

@Configuration(proxyBeanMethods = false)
@Import(EnableWebMvcConfiguration.class)
@EnableConfigurationProperties({ WebMvcProperties.class, WebProperties.class })
@Order(0)
public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer, ServletContextAware {}

WebMvcProperties.class和spring.mvc进行了绑定,WebProperties和spring.web进行了绑定

​编辑

​编辑

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

有参构造的所有参数的值都会在构造器中确定

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

分析:

 禁用静态资源

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
	if (!this.resourceProperties.isAddMappings()) {
		logger.debug("Default resource handling disabled");
		return;
	}
	addResourceHandler(registry, "/webjars/**", "classpath:/META-INF/resources/webjars/");
	addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) -> {
				registration.addResourceLocations(this.resourceProperties.getStaticLocations());
		if (this.servletContext != null) {
			ServletContextResource resource = new ServletContextResource(this.servletContext, SERVLET_LOCATION);
			registration.addResourceLocations(resource);
				}
			});
		}

当add-mappings:false时,所有的静态资源都被禁用

​编辑

 设置缓存的时间

​编辑

 Webjars的规则

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
	if (!this.resourceProperties.isAddMappings()) {
		logger.debug("Default resource handling disabled");
		return;
   	}
	addResourceHandler(registry, "/webjars/**", "classpath:/META-INF/resources/webjars/");
	addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) -> {
				    
        registration.addResourceLocations(this.resourceProperties.getStaticLocations());
		if (this.servletContext != null) {
		ServletContextResource resource = new ServletContextResource(this.servletContext, SERVLET_LOCATION);
		registration.addResourceLocations(resource);
				}
			});
		}

搜索webjars的时候,会沿着该路径往下找

​编辑

欢迎页

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

这个地方是写死的,所以不能修改静态网页的前缀 

​编辑