(四)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>

原文地址:https://www.cnblogs.com/yang-young-young/p/9291654.html

时间: 2024-07-30 12:55:43

(四)SpringBoot与Web开发的相关文章

SpringBoot(四) -- SpringBoot与Web开发

一.发开前准备 1.创建一个SpringBoot应用,引入我们需要的模块 2.SpringBoot已经默认将这些场景配置好了,只需要在配置文件中指定少量配置,就能运行起来 3.编写业务代码 二.静态资源映射规则 在WebMvcAutoConfiguration中有着如下的配置: 1 @Override 2 public void addResourceHandlers(ResourceHandlerRegistry registry) { 3 if (!this.resourceProperti

SpringBoot的Web开发

Web开发是开发中至关重要的一部分,web开发的核心内容主要包括servelet容器和SpringMVC. 1.SpringBoot的Web开发支持. SpringBoot提供了spring-boot-starter-web为web开发予以支持,spring-boot-starter-web提供了内嵌的Tomcat以及SpringMVC的依赖 而web相关的自动配置存储在spring-boot-autoconfigure.jar的org.srpingframework.boot.autoconf

4.SpringBoot的web开发1

一.回顾 好的,同学们,那么接下来呢,我们开始学习SpringBoot与Web开发,从这一章往后,就属于我们实战部分的内容了: 其实SpringBoot的东西用起来非常简单,因为SpringBoot最大的特点就是自动装配. 使用SpringBoot的步骤: 创建一个SpringBoot应用,选择我们需要的模块,SpringBoot就会默认将我们的需要的模块自动配置好 手动在配置文件中配置部分配置项目就可以运行起来了 专注编写业务代码,不需要考虑以前那样一大堆的配置了. 要熟悉掌握开发,之前学习的

SpringBoot与Web开发

web开发1).创建SpringBoot应用,选中我们需要的模块:2).SpringBoot已经默认将这些场景已经配置好了,只需要在配置文件中指定少量配置就可以运行起来3).自己编写业务代码: 自动配置原理?这个场景SpringBoot帮我们配置了扫码?能不能修改?能不能改哪些配置?能不能扩展?xxxxxxAutoConfiguration:帮我们给容器中自动配置组件:xxxProperties:配置类来 封装配置文件的内容: 2.SpringBoot对静态资源的 映射规则 @Configura

【SpringBoot】Web开发

一.简介 1.1 引入SpringBoot模块 1.2 SpringBoot对静态资源的映射规则 二.模版引擎 2.1 简介 2.2 引入thymeleaf 2.3 Thymeleaf使用 一.简介 1.1 引入SpringBoot模块 在介绍Web开发模块之前,先总结一下SpringBoot中如何引入某一个模块,我们知道,SpringBoot将功能模块封装为一个个的Starter : 1).创建SpringBoot应用,选中我们需要的模块; 2).SpringBoot已经默认将这些场景配置好了

SpringBoot 基本web开发demo

1.在创建的springboot项目中的pom.xml中导入Lombok的依赖 <dependency>    <groupId>org.projectlombok</groupId>    <artifactId>lombok</artifactId>    <version>1.18.6</version></dependency> 2.安装Lombok插件 3.在主启动类的同级创建实体类的包,在包中创建实

springboot用于web开发

1.使用SpringBoot:1)创建SpringBoot应用,选中我们需要的模块:2)SpringBoot已经默认将这些场景配置好了,只需要在配置文件中指定少量配置就可以运行起来3)自己编写业务代码: 自动配置原理? xxxxAutoConfiguration:帮我们给容器中自动配置组件:xxxxProperties:配置类来封装配置文件的内容: 2.SpringBoot对静态资源的映射规则 2.1"/**" 访问当前项目的任何资源,都去(静态资源的文件夹)找映射 "cla

Springboot整合web开发

一,整合 Servlet1,通过注解扫描完成 Servlet 组件的注册1.1 编写 servlet 1 /** 2 * 3 * springboot整合servlet方式一 4 * @author java 5 *<servlet> 6 * <servlet-name>FirstServletController</servlet-name> 7 * <servlet-class>com.zzp.controller.FirstServletControl

SpringBoot整合WEB开发--(八)启动任务系统

简介: 有一些特殊的任务需要在系统启动时执行,例如配置文件的加载,数据库初始化等操作,如果没有使用SpringBoot,这些问题可以在Listener中解决.SpringBoot提供了两种解决方案:CommandLineRunner和ApplicationRunner,这两个差别主要体现在参数上. 1.CommandLineRunner SpringBoot项目在启动时会遍历所有的CommandLineRunner的实现类并调用其中的run方法,如果整个系统中有多个CommandLineRunn