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 and beans.
- 内容协商视图解析器和BeanName视图解析器
- Support for serving static resources, including support for WebJars (covered later in this document)).
- 静态资源(包括webjars)
- Automatic registration of , , and beans.
- 自动注册
- Support for (covered later in this document).
- 支持 (后来我们配合内容协商理解原理)
- Automatic registration of (covered later in this document).
- 自动注册 (国际化用)
- Static support.
- 静态index.html 页支持
- Custom support (covered later in this document).
- 自定义
- Automatic use of a bean (covered later in this document).
- 自动使用 ,(DataBinder负责将请求数据绑定到JavaBean上)
If you want to keep those Spring Boot MVC customizations and make more MVC customizations (interceptors, formatters, view controllers, and other features), you can add your own class of type but without .
不用@EnableWebMvc注解。使用 + 自定义规则
If you want to provide custom instances of , , or , and still keep the Spring Boot MVC customizations, you can declare a bean of type and use it to provide custom instances of those components.
声明 改变默认底层组件
If you want to take complete control of Spring MVC, you can add your own annotated with , or alternatively add your own -annotated as described in the Javadoc of .
使用
静态资源访问
静态资源目录
只要静态资源放在类路径下: called (or or or
访问的路径为 : 当前项目根路径/ + 静态资源名
原理: 静态映射/。
请求进来,先去找Controller看能不能处理。不能处理的所有请求又都交给静态资源处理器。静态资源也找不到则响应404页面
改变默认的静态资源路径(静态资源访问添加前缀)
静态资源访问前缀
默认无前缀
当前项目 + static-path-pattern + 静态资源名 = 静态资源文件夹下找(localhost:8080/res/静态资源)
webjar(使用较少可以用来引入静态资源)
自动映射 /webjars/
https://www.webjars.org/
访问地址:http://localhost:8080/webjars/jquery/3.5.1/jquery.js 后面地址要按照依赖里面的包路径
欢迎页的支持
springboot支持
- 静态资源路径下 index.html
- 可以配置静态资源路径
- 但是不可以配置静态资源的访问前缀。否则导致 index.html不能被默认访问
- controller能处理/index
favicon.ico 放在静态资源目录下即可。
- SpringBoot启动默认加载 xxxAutoConfiguration 类(自动配置类)
- SpringMVC功能的自动配置类 WebMvcAutoConfiguration,生效
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-51xQ9VYF-62)(C:UsersLenovoAppDataRoamingTypora ypora-user-imagesimage-054823.png)]
- 给容器中配置了什么
- 配置文件的相关属性和xxx进行了绑定。WebMvcProperties==spring.mvc、ResourceProperties==spring.resources
配置类只有一个有参构造器
资源处理的默认规则
下面的源码为默认的静态资源的4个路径
欢迎页的处理规则
springboot 请求参数处理
请求映射
rest使用与原理
- @xxxMapping;
- Rest风格支持(使用HTTP请求方式动词来表示对资源的操作)
- 以前:/getUser 获取用户 /deleteUser 删除用户 /editUser 修改用户 /saveUser 保存用户
- 现在: /user *GET-*获取用户 *DELETE-*删除用户 *PUT-*修改用户 *POST-*保存用户
- 核心Filter;HiddenHttpMethodFilter
- 用法: 表单method=post,隐藏域 _method=put
- SpringBoot中手动开启
- 扩展:如何把_method 这个名字换成我们自己喜欢的。
版权声明:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权、违法违规、事实不符,请将相关资料发送至xkadmin@xkablog.com进行投诉反馈,一经查实,立即处理!
转载请注明出处,原文链接:https://www.xkablog.com/rfx/20098.html