一、SpringBoot创建web开发(三部曲)

  1.快速构建SpringBoot项目,并以jar包的形式构建

  

  2.选择对应的功能模块 (选定场景,配置少量的配置就可运行,不配置有默认值)

  3.编写自己的逻辑代码

二、SpringBoot对静态资源的映射规则

  通过查看WebMvcAutoConfiguration类,可以查看SpringBoot对静态资源存放的位置

     @Override

public void addResourceHandlers(ResourceHandlerRegistry registry) {   //添加资源映射
if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
return;
}
Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
CacheControl cacheControl = this.resourceProperties.getCache()
.getCachecontrol().toHttpCacheControl();
if (!registry.hasMappingForPattern("/webjars/**")) {
customizeResourceHandlerRegistration(registry
.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/")   //webjars/**都存放在classpath:
                                                   /META-INF/resources/webjars
.setCachePeriod(getSeconds(cachePeriod))
.setCacheControl(cacheControl));
}
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
if (!registry.hasMappingForPattern(staticPathPattern)) {
customizeResourceHandlerRegistration(
registry.addResourceHandler(staticPathPattern)      // /**表示访问项目的任何资源,都去(静态资源文件夹)
.addResourceLocations(getResourceLocations(
this.resourceProperties.getStaticLocations()))
.setCachePeriod(getSeconds(cachePeriod))
.setCacheControl(cacheControl));
}
}

  1.既所有的webjars/**资源,都在classpath:/META-INF/resources/webjars中找资源

  什么是webjars?  就是以jar形式的静态资源(如jquery.js)https://www.webjars.org/

  

  例如可以这么访问:localhost:8080/webjars/jquery/3.3.1-2/jquery.js

  2.、/**  表示访问当前项目的任何资源,都去(静态资源文件夹中寻找)

    静态资源文件夹(存放js,css,image等不包括html,html需要使用模板引擎)

  以下是静态资源文件夹的位置

  

    

  classpath:/META-INF/resources/,
classpath:/resources/,
classpath:/static/,
classpath:/public/"
  "/":  项目的根路径

   3.欢迎页(首页)

    

  

    欢迎页:静态资源文件夹下的所有index.html,会被显示出来。名字固定。

    

 

  4.项目工程的图标

  

      图标:在静态资源文件夹中存放.ico文件即可显示该图标。固定名字favicon.ico

  

三、模板引擎

  在SpringBoot中不用Jsp页面,而是使用thmeleaf模板。原因是语法更加简单,强大。

    

  使用步骤:

    1.在pom.xml中引入thmeleaf约束。将SpringBoot自带的thmeleaf版本覆盖带,使用版本3。   

  
  <!--切换thmeleaf版本-->
  <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>
  <properties>
<thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
<!-- 布局功能的支持程序 thymeleaf3主程序 layout2以上版本 -->
<!-- thymeleaf2 layout1-->
<thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version>
</properties>

   2.对thmeleaf的使用

  只要将普通的html页面放在classpath:/templates/   thmeleaf模板引擎就会自动渲染

  

Spring Boot web简介及原理 day04的更多相关文章

  1. Spring Boot 文件上传原理

    首先我们要知道什么是Spring Boot,这里简单说一下,Spring Boot可以看作是一个框架中的框架--->集成了各种框架,像security.jpa.data.cloud等等,它无须关 ...

  2. Springboot 系列(七)Spring Boot web 开发之异常错误处理机制剖析

    前言 相信大家在刚开始体验 Springboot 的时候一定会经常碰到这个页面,也就是访问一个不存在的页面的默认返回页面. 如果是其他客户端请求,如接口测试工具,会默认返回JSON数据. { &quo ...

  3. Springboot 系列(五)Spring Boot web 开发之静态资源和模版引擎

    前言 Spring Boot 天生的适合 web 应用开发,它可以快速的嵌入 Tomcat, Jetty 或 Netty 用于包含一个 HTTP 服务器.且开发十分简单,只需要引入 web 开发所需的 ...

  4. Spring Boot Web 开发@Controller @RestController 使用教程

    在 Spring Boot 中,@Controller 注解是专门用于处理 Http 请求处理的,是以 MVC 为核心的设计思想的控制层.@RestController 则是 @Controller ...

  5. Spring Boot 自动配置的原理、核心注解以及利用自动配置实现了自定义 Starter 组件

    本章内容 自定义属性快速入门 外化配置 自动配置 自定义创建 Starter 组件 摘录:读书是读完这些文字还要好好用心去想想,写书也一样,做任何事也一样 图 2 第二章目录结构图 第 2 章 Spr ...

  6. Spring Boot Web Executable Demo

    Spring Boot Web Executable Demo */--> pre.src {background-color: #292b2e; color: #b2b2b2;} pre.sr ...

  7. Springboot 系列(六)Spring Boot web 开发之拦截器和三大组件

    1. 拦截器 Springboot 中的 Interceptor 拦截器也就是 mvc 中的拦截器,只是省去了 xml 配置部分.并没有本质的不同,都是通过实现 HandlerInterceptor ...

  8. Spring Boot Web应用开发 CORS 跨域请求支持:

    Spring Boot Web应用开发 CORS 跨域请求支持: 一.Web开发经常会遇到跨域问题,解决方案有:jsonp,iframe,CORS等等CORS与JSONP相比 1. JSONP只能实现 ...

  9. 转-spring boot web相关配置

    spring boot web相关配置 80436 spring boot集成了servlet容器,当我们在pom文件中增加spring-boot-starter-web的maven依赖时,不做任何w ...

随机推荐

  1. 【原创】大数据基础之Hive(2)Hive SQL执行过程之SQL解析过程

    Hive SQL解析过程 SQL->AST(Abstract Syntax Tree)->Task(MapRedTask,FetchTask)->QueryPlan(Task集合)- ...

  2. 【原创】大叔经验分享(25)hive通过外部表读写hbase数据

    在hive中创建外部表: CREATE EXTERNAL TABLE hive_hbase_table(key string, name string,desc string) STORED BY ' ...

  3. 用WKWebView 截取整个Html页面

    以前使用UIWebview时,想截取整个页面,可以调整内部scrollView的frame,之后调用 scrollView的layer的 render 方法,很方便. 但是在WKWebView上,行不 ...

  4. uni-app图片压缩转base64位 利用递归来实现多张图片压缩

    //选择图片 chooseImage(){ let that =this uni.chooseImage({ sizeType: ['original','compressed'], //可以指定是原 ...

  5. Connet Scanning

    1.connect scanning with Scapy,   Tools that perform  Tcp scans operate by performing a full there-wa ...

  6. mysql查看索引与锁

    http://www.cnblogs.com/cocos/archive/2011/05/06/2039428.html Mysql乐观锁与悲观锁 http://www.cnblogs.com/esi ...

  7. CodeForces 286E Ladies' Shop 多项式 FFT

    原文链接http://www.cnblogs.com/zhouzhendong/p/8781889.html 题目传送门 - CodeForces 286E 题意 首先,给你$n$个数(并告诉你$m$ ...

  8. P1341 无序字母对 欧拉回路

    题目描述 给定n个各不相同的无序字母对(区分大小写,无序即字母对中的两个字母可以位置颠倒).请构造一个有n+1个字母的字符串使得每个字母对都在这个字符串中出现. 输入输出格式 输入格式: 第一行输入一 ...

  9. Eclipse导入maven项目时,pom-xml文件报错处理方法

    报错如下: Cannot read lifecycle mapping metadata for artifact org.apache.maven.plugins 解决方法: 出现该错误是因为jar ...

  10. Spring Cloud微服务笔记(三)服务治理:Spring Cloud Eureka快速入门

    服务治理:Spring Cloud Eureka 一.服务治理 服务治理是微服务架构中最为核心和基础的模块,它主要用来实现各个微服务实例的自动化注册与发现. 1.服务注册: 在服务治理框架中,通常会构 ...