springBoot静态资源访问规则

1.静态资源目录

默认访问类路径下 called /static (or /public or /resources or /META-INF/resources)这些包下的静态资源

访问:当前项目路径+静态资源名

默认静态映射 /**

请求进入 先去controller看是否能够处理 ,然后把所有不能处理的请求交给静态资源处理器

改变静态默认资源路径

spring.web.resources.static-locations=classpath:/xx/**

2.静态资源访问的前缀

默认无前缀

spring.mvc.static-path-pattern=/resources/**

3.静态资源管理

启动时

WebMvcAutoConfiguration类被加载
@EnableConfigurationProperties({ WebMvcProperties.class,
			org.springframework.boot.autoconfigure.web.ResourceProperties.class, WebProperties.class })
	@Order(0)
	public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer, ServletContextAware {

将配置文件中的值注入到类中

WebMvcProperties == spring.mvc  ResourceProperties== spring.resources

然后在该方法中进行映射

@Override
		public void addResourceHandlers(ResourceHandlerRegistry registry) {
            //当isAddMappings为false时 不开启资源映射
			if (!this.resourceProperties.isAddMappings()) {
				logger.debug("Default resource handling disabled");
				return;
			}
            //webjars资源映射

			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);
				}
			});
		}
进入getStaticLocations()方法 细看属性默认值
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/",
				"classpath:/resources/", "classpath:/static/", "classpath:/public/" };

		/**
		 * Locations of static resources. Defaults to classpath:[/META-INF/resources/,
		 * /resources/, /static/, /public/].
		 */
		private String[] staticLocations = CLASSPATH_RESOURCE_LOCATIONS;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值