Spring Boot学习笔记06——web开发01(静态资源映射)

1. 开发前的思考

在开发前我们要先对自动装配原理和开发的内容要有一定的认识,这样开发起来才能更高效和清晰

1.1 使用springboot
  1. 创建springboot应用,选中我们所需的模块依赖
  2. springboot已默认将 这些场景配置好了,我们只需在配置文件中指定少量配置即可运行起来
  3. 编写业务代码

自动装配原理:这个场景springboot帮我们配置了什么?能不能修改?能修改哪些配置?能不能拓展…
xxxAutoConfiguration:帮我们给容器中自动配置组件
xxxProperties:配置类来封装配置文件的内容

1.2 资源准备

我们还要准备一些静态的页面资源,我们可以前往getbootstrap下载,如果找不到的小伙伴可以私聊我获取
页面资源


2. springboot对静态资源的映射规则

我们打开WebMvcAutoConfiguration源码,可以看到addResourceHandlers方法

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		if (!this.resourceProperties.isAddMappings()) {
			logger.debug("Default resource handling disabled");
			return;
		}
		Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
		CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
		if (!registry.hasMappingForPattern("/webjars/**")) {
				customizeResourceHandlerRegistration(registry.addResourceHandler("/webjars/**")
					.addResourceLocations("classpath:/META-INF/resources/webjars/")
						.setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
		}
		String staticPathPattern = this.mvcProperties.getStaticPathPattern();
		if (!registry.hasMappingForPattern(staticPathPattern)) {
				customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern)
						.addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations()))
						.setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
		}
	}

通过源码我们可以看见,所有的/webjars/**都去classpath:/META-INF/resources/webjars/找资源(webjars表示以jar包方式映入静态资源)
我们可以去webjars官网找到对应资源的各版本maven依赖

导入对应依赖后,我们可以找到上面所说的资源路径,只需通过http://localhost:8080/webjars/jquery/3.3.1/jquery.js即可访问

我们可以到ResourceProperties中设置和静态资源有关的参数,如缓存时间等
@ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false)
public class ResourceProperties {

2.1 静态资源访问路径
"classpath:/META-INF/resources/", 
"classpath:/resources/",
"classpath:/static/", 
"classpath:/public/" 
"/":当前项目的根路径

classpath:表示resources资源路径,静态资源都可以放到上面列出来的路径来访问


2.2 设置欢迎页
	@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;
	}

spring boot默认是没有index页面的,我们可以在静态资源访问路径中添加一个index.html就可以给项目添加首页了


2.3 设置网站图标
	@Bean
		public SimpleUrlHandlerMapping faviconHandlerMapping() {
			SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
			mapping.setOrder(Ordered.HIGHEST_PRECEDENCE + 1);
			mapping.setUrlMap(Collections.singletonMap("**/favicon.ico",
					faviconRequestHandler()));
			return mapping;
		}

我们也可以给网站设置自定义图标,将名称和后缀名为favicon.ico的图标放在静态资源访问路径下即可

2.4 更改首页和图标效果展示


如果设置无效,则用CTRL+F5进行刷新


自定义静态资源访问路径

如果我们像对静态资源访问路径进行自定义设置,只需在properties配置文件中设置spring.resources.static-location=classpath:/xxx,classpath:/yyy即可(这里列举的是设置多个静态资源访问路径)


该SpringBoot学习笔记学习自雷神前辈,是对知识点的整理和自我认识的梳理,如有不当之处,欢迎指出

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值