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

举报
游坦之 发表于 2022/10/20 12:38:49 2022/10/20
【摘要】 本文已参与[新人创作礼]活动,一起开启掘金创作之路。​ 欢迎页配置默认放在static文件夹下,index.html即为欢迎页 手动修改欢迎页的位置在application.yaml文件中配置如下spring: web: resources: static-locations: [classpath:/haha/]​编辑​编辑 注意:可以配置静态资源路径,但不可以配置静态资...

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

欢迎页配置

默认放在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;
}

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

​编辑

【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。