一、工作流程
①原理图
②简易图
③语言描述
1、客户端: 用户向服务端发送一次请求,这个请求会先到前端控制器DispatcherServlet(也叫中央控制器)
2、前端控制器(DispatcherServlet): 接收到请求后会调用处理器映射器(HandlerMapping)。
3、处理器映射器(HandlerMapping): 根据请求中的URL,找到对应的Controller,返回给前端控制器。
4、处理器适配器(HandlerAdapter): 前端控制器知道要执行哪个Contronller,但是它只是把信息转发给处理器适配器,处理器适配器调用程序写好的Contronller,Contronller执行完,返回一个ModelAndView对象给适配器,处理器适配器再把这个对象返回给前端控制器
5、后端处理器(Handler): 后端处理器,对用户具体请求进行处理,也就是我们编写的 Controller 类。
6、前端控制器(DispatcherServlet): 将ModelAndView交给视图解析器解析(ViewReslover),然后返回真正的视图。
7、视图view: 前端控制器请求进行视图渲染,把model数据填充到request域,返回视图(jsp、html等)
8、前端控制器: 把视图响应返回给用户
④组件说明
- DispatcherServlet: 前端控制器,也称为中央控制器,它是整个请求响应的控制中心,组件的调用由它统一调度。
- HandlerMapping :处理器映射器,它根据用户访问的 URL 映射到对应的后端处理器 Handler。也就是说它知道处理用户请求的后端处理器,但是它并不执行后端处理器,而是将处理器告诉给中央处理器。
- HandlerAdapter: 处理器适配器,它调用后端处理器中的方法,返回逻辑视图 ModelAndView 对象。
- ViewResolver: 视图解析器,将 ModelAndView 逻辑视图解析为具体的视图(如 JSP)。
- Handler: 后端处理器,对用户具体请求进行处理,也就是我们编写的 Controller 类。
二、SpringMVC核心配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 配置@Controller注解扫描 -->
<context:component-scan base-package="cn.itheima.controller"></context:component-scan>
<!-- 注解驱动:
作用:替我们自动配置最新版的注解的处理器映射器和处理器适配器
-->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 配置视图解析器
作用:在controller中指定页面路径的时候就不用写页面的完整路径名称了,可以直接写页面去掉扩展名的名称
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 真正的页面路径 = 前缀 + 去掉后缀名的页面名称 + 后缀 -->
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"></property>
<!-- 后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>