SpringBoot的Web开发
一、创建Web项目
创建的时候勾选对应web选项即可,会自动引入相应的starter,pom如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
里面的具体依赖不一一展开了。
二、SpringBoot的Web自动配置
经过前面的研究,知道主要是springboot为我们引入了org.springframework.boot.autoconfigure.web包的
1.自动配置类:xxxxAutoConfiguration帮我们给容器中自动配置组件;
2.配置文件封装类:xxxxProperties:配置类来封装配置文件的内容;
eg:DispatcherServletAutoConfiguration、WebMvcAutoConfiguration和WebMvcProperties、ResourceProperties
具体自动配置帮我们做了什么可以看xxxxAutoConfiguration,我们配置文件能配置什么属性可以看xxxxProperties。
三、springboot web的静态资源映射规则
1.ResourceProperties
@ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false)
public class ResourceProperties { private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
"classpath:/META-INF/resources/", "classpath:/resources/",
"classpath:/static/", "classpath:/public/" }; /**
* Locations of static resources. Defaults to classpath:[/META-INF/resources/,
* /resources/, /static/, /public/].
*/
private String[] staticLocations = CLASSPATH_RESOURCE_LOCATIONS; /**
* Whether to enable default resource handling.
*/
private boolean addMappings = true; private final Chain chain = new Chain(); private final Cache cache = new Cache(); public String[] getStaticLocations() {
return this.staticLocations;
} public void setStaticLocations(String[] staticLocations) {
this.staticLocations = appendSlashIfNecessary(staticLocations);
} private String[] appendSlashIfNecessary(String[] staticLocations) {
String[] normalized = new String[staticLocations.length];
for (int i = 0; i < staticLocations.length; i++) {
String location = staticLocations[i];
normalized[i] = location.endsWith("/") ? location : location + "/";
}
return normalized;
}
可以看到springboot自动配置默认回到
"classpath:/META-INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"
目录下寻找静态资源。
2.WebMvcAuotConfiguration
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
return;
}
Integer cachePeriod = this.resourceProperties.getCachePeriod();
if (!registry.hasMappingForPattern("/webjars/**")) {
customizeResourceHandlerRegistration(
registry.addResourceHandler("/webjars/**")
.addResourceLocations(
"classpath:/META-INF/resources/webjars/")
.setCachePeriod(cachePeriod));
}
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
//静态资源文件夹映射
if (!registry.hasMappingForPattern(staticPathPattern)) {
customizeResourceHandlerRegistration(
registry.addResourceHandler(staticPathPattern)
.addResourceLocations(
this.resourceProperties.getStaticLocations())
.setCachePeriod(cachePeriod));
}
} //配置欢迎页映射
@Bean
public WelcomePageHandlerMapping welcomePageHandlerMapping(
ResourceProperties resourceProperties) {
return new WelcomePageHandlerMapping(resourceProperties.getWelcomePage(),
this.mvcProperties.getStaticPathPattern());
}
private Optional<Resource> getWelcomePage() {
String[] locations = getResourceLocations(
this.resourceProperties.getStaticLocations());
return Arrays.stream(locations).map(this::getIndexHtml)
.filter(this::isReadable).findFirst();
}
private Resource getIndexHtml(String location) {
return this.resourceLoader.getResource(location + "index.html");
}
//配置喜欢的图标
@Configuration
@ConditionalOnProperty(value = "spring.mvc.favicon.enabled", matchIfMissing = true)
public static class FaviconConfiguration { private final ResourceProperties resourceProperties; public FaviconConfiguration(ResourceProperties resourceProperties) {
this.resourceProperties = resourceProperties;
} @Bean
public SimpleUrlHandlerMapping faviconHandlerMapping() {
SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
mapping.setOrder(Ordered.HIGHEST_PRECEDENCE + 1);
//所有 **/favicon.ico
mapping.setUrlMap(Collections.singletonMap("**/favicon.ico",
faviconRequestHandler()));
return mapping;
} @Bean
public ResourceHttpRequestHandler faviconRequestHandler() {
ResourceHttpRequestHandler requestHandler = new ResourceHttpRequestHandler();
requestHandler
.setLocations(this.resourceProperties.getFaviconLocations());
return requestHandler;
} }
1.webjars:允许我们以jar的方式引入静态资源(http://www.webjars.org/),如jQuery
<!--引入jquery-webjar-->在访问的时候只需要写webjars下面资源的名称即可
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.3.1</version>
</dependency>
访问路径:localhost:8080/webjars/jquery/3.3.1/jquery.js
2.欢迎页,默认静态资源根目录/ndex.html
3.页面标签页图标,默认静态资源根目录/favicon.ico
四、Web自定义设置
以上这些都是了解到的默认配置,但有的时候不满足实际需求需要我们自定义配置,下节边自定义边研究原理。
SpringBoot的Web开发的更多相关文章
- SpringBoot学习(七)-->SpringBoot在web开发中的配置
SpringBoot在web开发中的配置 Web开发的自动配置类:在Maven Dependencies-->spring-boot-1.5.2.RELEASE.jar-->org.spr ...
- SpringBoot:Web开发
西部开源-秦疆老师:基于SpringBoot 2.1.6 的博客教程 , 基于atguigu 1.5.x 视频优化 秦老师交流Q群号: 664386224 未授权禁止转载!编辑不易 , 转发请注明出处 ...
- SpringBoot之WEB开发-专题二
SpringBoot之WEB开发-专题二 三.Web开发 3.1.静态资源访问 在我们开发Web应用的时候,需要引用大量的js.css.图片等静态资源. 默认配置 Spring Boot默认提供静态资 ...
- springboot java web开发工程师效率
基础好工具 idea iterm2 和 oh-my-zsh git 热加载 java web项目每次重启时间成本太大. 编程有一个过程很重要, 就是试验, 在一次次试验中探索, 积累素材优化调整程序模 ...
- SpringBoot与Web开发
web开发1).创建SpringBoot应用,选中我们需要的模块:2).SpringBoot已经默认将这些场景已经配置好了,只需要在配置文件中指定少量配置就可以运行起来3).自己编写业务代码: 自动配 ...
- SpringBoot日记——Web开发篇
准备开始实战啦!~~~~ 我们先来看,SpringBoot的web是如何做web开发的呢?通常的步骤如下: 1.创建springboot应用,指定模块: 2.配置部分参数配置: 3.编写业务代码: 为 ...
- 十二、springboot之web开发之静态资源处理
springboot静态资源处理 Spring Boot 默认为我们提供了静态资源处理,使用 WebMvcAutoConfiguration 中的配置各种属性. 建议大家使用Spring Boot的默 ...
- SpringBoot(四) Web开发 --- Thymeleaf、JSP
Spring Boot提供了spring-boot-starter-web为Web开发予以支持,spring-boot-starter-web为我们提供了嵌入的Tomcat以及Spring MVC的依 ...
- 【SpringBoot】Web开发
一.简介 1.1 引入SpringBoot模块 1.2 SpringBoot对静态资源的映射规则 二.模版引擎 2.1 简介 2.2 引入thymeleaf 2.3 Thymeleaf使用 一.简介 ...
随机推荐
- Vim-latex 插件 的安装
ref:https://www.jianshu.com/p/ddd825064062 Vim-latex 插件 1. 安装 Vim-latex 插件是一个强大的Latex插件, 它的安装方法是: 将下 ...
- Linux 文件格式转码工具
Linux 系统下文件编码转换格式工具 ICONV 下载 https://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.15.tar.gz 源码安装: $ ./con ...
- 深入web的请求过程
一.深入web的请求过程 1.1.B/S网络架构概述 · 从前端到后端,都基于应用层协议HTTP来交互数据.一个请求就对应了一个操作,完成操作之后就断开了连接.基于这样的特点可以用来满足海量的用户的操 ...
- SpringMVC+Apache Shiro+JPA(hibernate)整合配置
序: 关于标题: 说是教学,实在愧不敢当,但苦与本人文笔有限,实在找不到更合理,谦逊的词语表达,只能先这样定义了. 其实最真实的想法,只是希望这个关键词能让更多的人浏览到这篇文章,也算是对于自己写文章 ...
- mac charles手机抓包详细教程
1.官方下载charles 2.查看电脑IP地址 3.Proxy>Proxy Settings>勾选 Enable transparent HTTP proxying (记住端口号 88 ...
- MFC常用宏
MFC调试宏 TRACE() 其形式与函数printf()的参数一样,功能是在调试运行时把表达式的值输出到Output调试窗口. Debug版有效 ASSERT()——断言宏, 表达式为真,则程 ...
- 题解 洛谷P3936 Coloring
考虑搜索,发现复杂度爆炸 贪心,正确性过低(~~实测爆炸~~) 于是,~~发现~~这题是模拟退火 这里不讲解退火的定义了,初学退火可以去平衡点 退火本身维护一个答案图像,答案的q,当前图 ...
- springboot整合springdata-jpa
1.简介 SpringData : Spring 的一个子项目.用于简化数据库访问,支持NoSQL 和 关系数据存储.其主要目标是使数据库的访问变得方便快捷. SpringData 项目所支持 No ...
- SQL总结 连表查询
连接查询包括合并.内连接.外连接和交叉连接,如果涉及多表查询,了解这些连接的特点很重要. 只有真正了解它们之间的区别,才能正确使用. 1.Union UNION 操作符用于合并两个或多个 SELECT ...
- PC设置局域网打印机
打印机采用局域网网络连接方式,下面以Windows系统为例说明如何添加此打印机. 将电脑接入局域网 在“控制面板”中打开“设备与打印机”,点击“添加打印机” 在弹出列表中,会自动出现打印机型号,选中它 ...