学习视频
尚硅谷雷神SpringBoot2零基础入门springboot全套完整版(spring boot2)
集数:22—25
学习笔记
【Java】学习笔记汇总
文章目录
- 一、SpringMVC自动配置概述
- 二、静态资源访问
- 2.1 静态资源目录
- 2.1.1 静态资源访问前缀
- 2.1.2 改变默认的静态资源路径
- 2.1.3 webjar
- 2.2 欢迎页支持
- 2.3 自定义 Favicon
- 2.4 静态资源配置原理
官方文档
Spring Boot provides auto-configuration for Spring MVC that works well with most applications.(大多场景我们都无需自定义配置)
The auto-configuration adds the following features on top of Spring’s defaults:
● Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans.
○ 内容协商视图解析器和BeanName视图解析器
● Support for serving static resources, including support for WebJars .
○ 静态资源(包括webjars)
● Automatic registration of Converter, GenericConverter, and Formatter beans.
○ 自动注册 Converter,GenericConverter,Formatter
● Support for HttpMessageConverters .
○ 支持 HttpMessageConverters
● Automatic registration of MessageCodesResolver .
○ 自动注册 MessageCodesResolver (国际化用)
● Static index.html support.
○ 静态index.html 页支持
● Custom Favicon support.
○ 自定义 Favicon
● Automatic use of a ConfigurableWebBindingInitializer bean .
○ 自动使用 ConfigurableWebBindingInitializer ,(DataBinder负责将请求数据绑定到JavaBean上)
二、静态资源访问
创建Springboot项目,添加以下依赖:
官方文档
/static
/public
/resources
/meta-INF/resources
访问: 当前项目根路径/ + 静态资源名
原理:静态映射请求为/**,当有请求进来,先去找Controller看能不能处理;不能处理的所有请求又都交给静态资源处理器;静态资源也找不到则响应404页面
2.1.1 静态资源访问前缀默认无前缀。
spring:
mvc:
static-path-pattern: /res/**
静态资源文件夹下找:当前项目 + static-path-pattern + 静态资源名
2.1.2 改变默认的静态资源路径spring:
mvc:
static-path-pattern: /res/**
web:
resources:
static-locations:
[ classpath:/haha/]
访问1.jpg:localhost:8080/res/1.jpg
把常见的静态资源文件转换为jar包,自动映射 /webjars/**
https://www.webjars.org/
org.webjars jquery 3.5.1
访问地址:localhost:8080/webjars/jquery/3.5.1/jquery.js
后面地址要按照依赖里面的包路径
2.2 欢迎页支持 方式一
静态资源路径下的index.html
○ 可以配置静态资源路径
○ 但是不可以配置静态资源的访问前缀。否则导致 index.html不能被默认访问
spring:
# mvc:
# static-path-pattern: /res/** 这个会导致welcome page功能失效
resources:
static-locations: [classpath:/haha/]
方式二
通过配置控制器:
@RestController
public class HelloController {
@RequestMapping("/")
public String index() {
return "Welcome!";
}
}
2.3 自定义 Favicon
favicon.ico 放在静态资源目录下即可。
# 静态页面访问前缀会导致 Favicon 功能失效 # spring: # mvc: # static-path-pattern: /res/**2.4 静态资源配置原理
略
集数:25



