springboot的web开发
一 自动配置原理
1)spingboot帮我们配置了什么?能不能修改?能修改哪些配置?xxx
xxxxAutoConfiguration帮我们往容器里添加组件
xxxxProperties配置类来封装配置文件内容
2)springboot对静态资源的映射规则
1. 所有web/jars/**,都去classpath:/META-INFO/resources/webjars/找资源。
2.webjars以jar包的形式引入静态资源。
例如,在pom.xml中引入jquery
<!--引入jquery的webjar包 --> <dependency> <groupId>org.webjars</groupId> <artifactId>jquery</artifactId> <version>3.4.0</version> </dependency>
------------------------------- 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(WebMvcAutoConfiguration.getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl)); } } }
http://localhost:8080/webjars/jquery/3.4.0/jquery.js可以访问。
二 、"/**"访问当前项目的任何资源,如果没有任何处理,则去下面的地址,进行访问(静态资源文件夹)
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
和当前项目的根路径。
三 欢迎页(首页)的配置
@Bean public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext, FormattingConversionService mvcConversionService, ResourceUrlProvider mvcResourceUrlProvider) {
(欢迎页)所有静态资源文件夹下的index.html,被/**映射
http://localhost:8080/找/**(根目录)下的index.html文件
四 配置图标
/**/favicon.ico都是在静态资源文件夹下找。
五 配置静态资源路径
在application.properties里追加
spring.resources.static-locations=classpath:/hello/,classpath:/yjg/
可以配置多个默认文件夹