业务流程图
架构(类图)
流程总结
DispatcherServlet:流程载体,首脑,总指挥官
Handler:具体处理请求
HandleMapping:匹配URL到具体的Handler
HandleAdapter:将匹配到的Handler适配至特定的接口
ModleAndView : 封装视图与数据
viewResolver:解析ModleAndView,得到具体的view
流程:
URL到达DispatcherServlet,
DispatcherServlet委派HandleMapping获取请求匹配到的Handler,
DispatcherServlet委派Handler处理请求获取ModelAndView,
DispatcherServlet委派viewResolver解析获取到的ModelAndView获得View,
DispatcherServlet使用Model渲染View返回客户端
问答
1 为什么请求会先到达DispatcherServlet?
简单的说是以上配置让tomcat将请求导向了这个DispatcherServlet
<servlet>
<servlet-name>xxx</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/META-INF/spring/front-web.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>xxx</servlet-name>
<url-patter`这里写代码片`n>/*</url-pattern>
</servlet-mapping>
2 HandleAdapter有什么用?
使用了适配器设计模式,目的是为了是程序在扩展时为了用一致的方式去处理请求,请求具体是由Handler处理的,Handler有很多种,如@RequestMapping和实现Controller接口,处理请求对于前者是执行被注解的方法,对于后者是调用Controller对象的handleRequest方法,形式不同,使用了HandleAdapter以后就请求处理程序就可以忽略这种不同,以后有新的Handler形式也不用修改程序,符合OCP原则
3 我的配置文件中没有配置 HandleMapping和HandleAdapter?
<mvc:annotation-driven/>
<!-- 由org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser解析
registers the following HandlerMappings:
RequestMappingHandlerMapping ordered at 0 for mapping requests to annotated controller methods.
BeanNameUrlHandlerMapping ordered at 2 to map URL paths to controller bean names.
Note: Additional HandlerMappings may be registered as a result of using the <view-controller> or the <resources> MVC namespace elements.
registers the following HandlerAdapters:
RequestMappingHandlerAdapter for processing requests with annotated controller methods.
HttpRequestHandlerAdapter for processing requests with HttpRequestHandlers.
SimpleControllerHandlerAdapter for processing requests with interface-based Controllers.
registers the following HandlerExceptionResolvers:
ExceptionHandlerExceptionResolver for handling exceptions through @ExceptionHandler methods.
ResponseStatusExceptionResolver for exceptions annotated with @ResponseStatus.
DefaultHandlerExceptionResolver for resolving known Spring exception types
-->
SpringMVC设计到的设计模式
HandleAdapter:适配器模式
viewResolver:模板方法