使用SpringBoot
- 创建Spring Boot应用,选择需要的模块
- SpringBoot会将这些默认场景配置好,只需要配置文件中指定少量配置就可以运行起来
- 编写业务逻辑代码
xxxxAutoConfiguration:帮助我们给容器中自动配置组件
xxxxProperties:配置类来封装配置文件的内容
Spring Boot对静态资源的映射规则
查看下列文件源码:
...
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();
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(getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
}
}
...
1. 所有的webjars/**:都去META-INF/resources/webjars找资源
webjars:以jar包的方式引入静态资源(www.webjars.org)![在这里插入图片描述](https://img-blog.csdnimg.cn/20190702170237215.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM0NTk4NjY3,size_16,color_FFFFFF,t_70)
查看jquery静态文件:
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190702170440782.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM0NTk4NjY3,size_16,color_FFFFFF,t_70)
从浏览器访问静态文件jquery
http://localhost:8080/webjars/jquery/3.4.1/jquery.min.js
2. /**:访问当前项目的任何资源
classpath:/META-INF/resources/*
classpath:/resources/*
classpath:/static/*
classpath:/public/*
"/" 当前项目的类路径
例如:在resources/static下新建demo/demo.js
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190702172202316.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM0NTk4NjY3,size_16,color_FFFFFF,t_70)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190702172218187.png)
欢迎页:静态资源文件夹下所有的index.html页面
localhost:8080 —>找index.html