SpringMVC实现原理及详解
1、Spring mvc介绍
SpringMVC框架是以请求为驱动,围绕Servlet设计,将请求发给控制器,然后通过模型对象,分派器来展示请求结果视图。其中核心类是DispatcherServlet,它是一个Servlet,顶层是实现的Servlet接口。
2、SpringMVC使用
需要在web.xml中配置DispatcherServlet。并且需要配置spring监听器ContextLoaderListene
1 <listener> 2 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 3 </listener> 4 <servlet> 5 <servlet-name>springmvc</servlet-name> 6 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 7 <!-- 如果不设置init-param标签,则必须在/WEB-INF/下创建xxx-servlet.xml文件,其中xxx是servlet-name中配置的名称。 --> 8 <init-param> 9 <param-name>contextConfigLocation</param-name> 10 <param-value>classpath:spring/springmvc-servlet.xml</param-value> 11 </init-param> 12 <load-on-startup>1</load-on-startup> 13 </servlet> 14 <servlet-mapping> 15 <servlet-name>springmvc</servlet-name> 16 <url-pattern>/</url-pattern> 17 </servlet-mapping>
3、SpringMVC运行原理
如图所示:
流程说明:
(1)客户端(浏览器)发送请求,直接请求到DispatcherServlet。
(2)DispatcherServlet根据请求信息调用HandlerMapping,解析请求对应的Handler。
(3)解析到对应的Handler后,开始由HandlerAdapter适配器处理。
(4)HandlerAdapter会根据Handler来调用真正的处理器开处理请求,并处理相应的业务逻辑。
(5)处理器处理完业务后,会返回一个ModelAndView对象,Model是返回的数据对象,View是个逻辑上的View。
(6)ViewResolver会根据逻辑View查找实际的View。
(7)DispaterServlet把返回的Model传给View。
(8)通过View返回给请求者(浏览器)
4、DispatcherServlet详细解析
首先看下源码:
1 package org.springframework.web.servlet; 2 3 @SuppressWarnings("serial") 4 public class DispatcherServlet extends FrameworkServlet { 5 6 public static final String MULTIPART_RESOLVER_BEAN_NAME = "multipartResolver"; 7 public static final String LOCALE_RESOLVER_BEAN_NAME = "localeResolver"; 8 public static final String THEME_RESOLVER_BEAN_NAME = "themeResolver"; 9 public static final String HANDLER_MAPPING_BEAN_NAME = "handlerMapping"; 10 public static final String HANDLER_ADAPTER_BEAN_NAME = "handlerAdapter"; 11 public static final String HANDLER_EXCEPTION_RESOLVER_BEAN_NAME = "handlerExceptionResolver"; 12 public static final String REQUEST_TO_VIEW_NAME_TRANSLATOR_BEAN_NAME = "viewNameTranslator"; 13 public static final String VIEW_RESOLVER_BEAN_NAME = "viewResolver"; 14 public static final String FLASH_MAP_MANAGER_BEAN_NAME = "flashMapManager"; 15 public static final String WEB_APPLICATION_CONTEXT_ATTRIBUTE = DispatcherServlet.class.getName() + ".CONTEXT"; 16 public static final String LOCALE_RESOLVER_ATTRIBUTE = DispatcherServlet.class.getName() + ".LOCALE_RESOLVER"; 17 public static final String THEME_RESOLVER_ATTRIBUTE = DispatcherServlet.class.getName() + ".THEME_RESOLVER"; 18 public static final String THEME_SOURCE_ATTRIBUTE = DispatcherServlet.class.getName() + ".THEME_SOURCE"; 19 public static final String INPUT_FLASH_MAP_ATTRIBUTE = DispatcherServlet.class.getName() + ".INPUT_FLASH_MAP"; 20 public static final String OUTPUT_FLASH_MAP_ATTRIBUTE = DispatcherServlet.class.getName() + ".OUTPUT_FLASH_MAP"; 21 public static final String FLASH_MAP_MANAGER_ATTRIBUTE = DispatcherServlet.class.getName() + ".FLASH_MAP_MANAGER"; 22 public static final String EXCEPTION_ATTRIBUTE = DispatcherServlet.class.getName() + ".EXCEPTION"; 23 public static final String PAGE_NOT_FOUND_LOG_CATEGORY = "org.springframework.web.servlet.PageNotFound"; 24 private static final String DEFAULT_STRATEGIES_PATH = "DispatcherServlet.properties"; 25 protected static final Log pageNotFoundLogger = LogFactory.getLog(PAGE_NOT_FOUND_LOG_CATEGORY); 26 private static final Properties defaultStrategies; 27 static { 28 try { 29 ClassPathResource resource = new ClassPathResource(DEFAULT_STRATEGIES_PATH, DispatcherServlet.class); 30 defaultStrategies = PropertiesLoaderUtils.loadProperties(resource); 31 } 32 catch (IOException ex) { 33 throw new IllegalStateException("Could not load 'DispatcherServlet.properties': " + ex.getMessage()); 34 } 35 } 36 37 /** Detect all HandlerMappings or just expect "handlerMapping" bean? */ 38 private boolean detectAllHandlerMappings = true; 39 40 /** Detect all HandlerAdapters or just expect "handlerAdapter" bean? */ 41 private boolean detectAllHandlerAdapters = true; 42 43 /** Detect all HandlerExceptionResolvers or just expect "handlerExceptionResolver" bean? */ 44 private boolean detectAllHandlerExceptionResolvers = true; 45 46 /** Detect all ViewResolvers or just expect "viewResolver" bean? */ 47 private boolean detectAllViewResolvers = true; 48 49 /** Throw a NoHandlerFoundException if no Handler was found to process this request? **/ 50 private boolean throwExceptionIfNoHandlerFound = false; 51 52 /** Perform cleanup of request attributes after include request? */ 53 private boolean cleanupAfterInclude = true; 54 55 /** MultipartResolver used by this servlet */ 56 private MultipartResolver multipartResolver; 57 58 /** LocaleResolver used by this servlet */ 59 private LocaleResolver localeResolver; 60 61 /** ThemeResolver used by this servlet */ 62 private ThemeResolver themeResolver; 63 64 /** List of HandlerMappings used by this servlet */ 65 private List<HandlerMapping> handlerMappings; 66 67 /** List of HandlerAdapters used by this servlet */ 68 private List<HandlerAdapter> handlerAdapters; 69 70 /** List of HandlerExceptionResolvers used by this servlet */ 71 private List<HandlerExceptionResolver> handlerExceptionResolvers; 72 73 /** RequestToViewNameTranslator used by this servlet */ 74 private RequestToViewNameTranslator viewNameTranslator; 75 76 private FlashMapManager flashMapManager; 77 78 /** List of ViewResolvers used by this servlet */ 79 private List<ViewResolver> viewResolvers; 80 81 public DispatcherServlet() { 82 super(); 83 } 84 85 public DispatcherServlet(WebApplicationContext webApplicationContext) { 86 super(webApplicationContext); 87 } 88 @Override 89 protected void onRefresh(ApplicationContext context) { 90 initStrategies(context); 91 } 92 93 protected void initStrategies(ApplicationContext context) { 94 initMultipartResolver(context); 95 initLocaleResolver(context); 96 initThemeResolver(context); 97 initHandlerMappings(context); 98 initHandlerAdapters(context); 99 initHandlerExceptionResolvers(context); 100 initRequestToViewNameTranslator(context); 101 initViewResolvers(context); 102 initFlashMapManager(context); 103 } 104 }
DispatcherServlet类中的属性beans:
HandlerMapping:用于handlers映射请求和一系列的对于拦截器的前处理和后处理,大部分用@Controller注解。
HandlerAdapter:帮助DispatcherServlet处理映射请求处理程序的适配器,而不用考虑实际调用的是 哪个处理程序。
HandlerExceptionResolver:处理映射异常。
ViewResolver:根据实际配置解析实际的View类型。
LocaleResolver:解决客户正在使用的区域设置以及可能的时区,以便能够提供国际化视野。
ThemeResolver:解决Web应用程序可以使用的主题,例如提供个性化布局。
MultipartResolver:解析多部分请求,以支持从HTML表单上传文件。
FlashMapManager:存储并检索可用于将一个请求属性传递到另一个请求的input和output的FlashMap,通常用于重定向。
在Web MVC框架中,每个DispatcherServlet都拥自己的WebApplicationContext,它继承了ApplicationContext。WebApplicationContext包含了其上下文和Servlet实例之间共享的所有的基础框架beans。
HandlerAdapter:
HandlerMapping接口处理请求的映射
HandlerMapping接口的实现类:
SimpleUrlHandlerMapping类通过配置文件把URL映射到Controller类。
DefaultAnnotationHandlerMapping类通过注解把URL映射到Controller类。
HandlerAdapter:
HandlerAdapter接口-处理请求映射
AnnotationMethodHandlerAdapter:通过注解,把请求URL映射到Controller类的方法上。
HandlerExceptionResolver:
HandlerExceptionResolver接口-异常处理接口
SimpleMappingExceptionResolver通过配置文件进行异常处理。
AnnotationMethodHandlerExceptionResolver:通过注解进行异常处理。
ViewResolver:
ViewResolver接口解析View视图。
UrlBasedViewResolver类 通过配置文件,把一个视图名交给到一个View来处理。
本文转载:https://blog.csdn.net/yanweihpu/article/details/80366218