Web 开发简介
# 160.Web 开发简介
接下来,我们就正式讲解 web 开发的场景
# 前言
该场景用的多,知识点多、细,底层原理复杂,因此我们讲的也多:
- SpringMVC 自动配置概览
- 简单功能分析
- 请求参数处理
- 数据响应与内容协商
- 视图解析与模板引擎
- 拦截器
- 异常处理
- 原生 Servlet 组件
- 嵌入式 web 容器
- 定制化原理
- .....
先讲怎么用,在讲原理
相关官方文档:
# 简介
首先,SpringBoot 是框架的框架,底层仍然用的是 SpringMVC,并且配置好了 SpringMVC;
那么具体配置了什么呢?我们来看看:
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
ContentNegotiatingViewResolverandBeanNameViewResolverbeans.- Support for serving static resources, including support for WebJars (covered later in this document (opens new window))).
- Automatic registration of
Converter,GenericConverter, andFormatterbeans.- Support for
HttpMessageConverters(covered later in this document (opens new window)).- Automatic registration of
MessageCodesResolver(covered later in this document (opens new window)).- Static
index.htmlsupport.- Custom
Faviconsupport (covered later in this document (opens new window)).- Automatic use of a
ConfigurableWebBindingInitializerbean (covered later in this document (opens new window)). 大意:
SpringBoot 给 SpringMVC,提供了自动配置,并且大多数场景下都能运作顺利。
自动配置添加了如下默认配置:
- 配置了内容协商视图解析器和 BeanName 视图解析器
- 支持静态资源(包括 webjars)
- 自动注册
Converter,GenericConverter,Formatter - 支持
HttpMessageConverters(后来我们配合内容协商理解原理) - 自动注册
MessageCodesResolver(用于国际化,一般用的少) - 静态 index.html 页支持
- 自定义
Favicon(图标) - 自动使用
ConfigurableWebBindingInitializer,(DataBinder 负责将请求数据绑定到 JavaBean 上) 也支持定制化:
If you want to keep those Spring Boot MVC customizations and make more MVC customizations (opens new window) (interceptors, formatters, view controllers, and other features), you can add your own
@Configurationclass of typeWebMvcConfigurerbut without@EnableWebMvc.If you want to provide custom instances of
RequestMappingHandlerMapping,RequestMappingHandlerAdapter, orExceptionHandlerExceptionResolver, and still keep the Spring Boot MVC customizations, you can declare a bean of typeWebMvcRegistrationsand use it to provide custom instances of those components.If you want to take complete control of Spring MVC, you can add your own
@Configurationannotated with@EnableWebMvc, or alternatively add your own@Configuration-annotatedDelegatingWebMvcConfigurationas described in the Javadoc of@EnableWebMvc.
大意:
不用@EnableWebMvc 注解,使用 @Configuration + WebMvcConfigurer 自定义规则(比如拦截器,格式化器,视图控制器)
声明 WebMvcRegistrations 改变默认底层组件
使用 ** ** @EnableWebMvc+@Configuration+DelegatingWebMvcConfiguration 全面接管SpringMVC