(四)SpringBoot与Web开发

1.简介

使用SpringBoot;

1.创建SpringBoot应用,选中我们需要的模块

2.SpringBoot已经默认将这些场景配置好了,只需要在配置文件中指定少量配置就可以运行起来

3.自己编写业务代码

自动配置原理?

这个场景SpringBoot帮我们配置了什么?能不能修改?能修改哪些配置?能不能扩展?

1 xxxAutoConfiguration:帮我们给容器中自动配置组件
2 xxxProperties:配置类来封装配置文件的内容
 //可以设置和静态资源有关的参数,缓存时间

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

1 @ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false)
2 public class ResourceProperties implements ResourceLoaderAware {
3   //可以设置和静态资源有关的参数,缓存时间等
 1 @Override
 2         public void addResourceHandlers(ResourceHandlerRegistry registry) {
 3             if (!this.resourceProperties.isAddMappings()) {
 4                 logger.debug("Default resource handling disabled");
 5                 return;
 6             }
 7             Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
 8             CacheControl cacheControl = this.resourceProperties.getCache()
 9                     .getCachecontrol().toHttpCacheControl();
10             if (!registry.hasMappingForPattern("/webjars/**")) {
11                 customizeResourceHandlerRegistration(registry
12                         .addResourceHandler("/webjars/**")
13                         .addResourceLocations("classpath:/META-INF/resources/webjars/")
14                         .setCachePeriod(getSeconds(cachePeriod))
15                         .setCacheControl(cacheControl));
16             }
17             String staticPathPattern = this.mvcProperties.getStaticPathPattern();
18             if (!registry.hasMappingForPattern(staticPathPattern)) {
19                 customizeResourceHandlerRegistration(
20                         registry.addResourceHandler(staticPathPattern)
21                                 .addResourceLocations(getResourceLocations(
22                                         this.resourceProperties.getStaticLocations()))
23                                 .setCachePeriod(getSeconds(cachePeriod))
24                                 .setCacheControl(cacheControl));
25             }
26         }

1.所有/webjars/**,都去classpath:/META-INF/resources/webjar/找资源;

  webjars:以jar包的方式引入静态资源

localhost:8080/jquery/3.3.1/jquery.js

<!-- 引入jquery-webjar -->在访问的时候只需要写webjars下面资源的名称即可
<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>jquery</artifactId>
    <version>3.3.1</version>
</dependency>

2."/**"访问当前项目的任何资源,(静态资源的文件夹)

1 "classpath:/META-INF/resources/", 
2 "classpath:/resources/",
3 "classpath:/static/", 
4 "classpath:/public/" 
5 "/":当前项目的根路径

localhost:8080/abc ===> 去静态资源文件夹里面找abc

3.欢迎页;静态资源文件夹下的所有index.html页面;被"/**"映射;

  localhost:8080/ 找index页面

4.所有的**/favicon.ico 都是在静态资源文件夹找

3.模板引擎

jsp、Velocity、Thymeleaf

SpringBoot推荐的Thymeleaf

语法更简单,功能更强大

1.引入thymeleaf

1 <dependency>
2             <groupId>org.springframework.boot</groupId>
3             <artifactId>spring-boot-starter-thymeleaf</artifactId>
4         </dependency>

切换thymeleaf版本

<thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
        <!-- 布局功能的支持程序 thymeleaf3主程序 layout2以上版本 -->
        <!-- thymeleaf2 layout1 -->
        <thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version>

猜你喜欢

转载自www.cnblogs.com/yang-young-young/p/9291654.html