SpringMVC架構實現原理
一、SpringMVC介紹
Spring mvc是一個基於mvc的web框架。其中核心類是DispatcherServlet,它是一個Servlet,頂層是實現的Servlet介面。
Spring mvc是spring框架的一個模組,springmvc和spring無需通過中間整合層進行整合。
二、SpringMVC使用
需要在web.xml中配置DispatcherServlet。並且需要配置spring監聽器ContextLoaderListener
<?xml version="1.0" encoding="UTF-8"?> <!-- 當前的XML文件版本與字元編碼 --> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>SpringMVC</display-name> <!-- SpringMVC前段控制器 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- contextConfigLocation配置SpringMVC載入的配置檔案(配置處理器對映器、介面卡等等) 如果不配置ContextConfigLocation,預設載入的是/WEB-INF/servlet名稱-servlet.xml(springmvc-servlet.xml) --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <!-- 第一種:*.action,訪問以.action結尾由DispatcherServlet進行解析 第二種:/,所以訪問的地址都由DispatcherServlet進行解析,對於靜態檔案的配置不讓DispatcherServlet解析 第三種:/*,這樣配置不對 --> <url-pattern>*.action</url-pattern> </servlet-mapping> </web-app>
三、SpringMVC執行原理
1、 執行流程圖:
2、流程說明:
(1)客戶端(瀏覽器)傳送請求,直接請求到DispatcherServlet。(請求DispatcherServlet)
(2)DispatcherServlet根據請求資訊呼叫HandlerMapping,解析請求對應的Handler。(查詢@Controller)
(3)解析到對應的Handler後,開始由HandlerAdapter介面卡處理。(查詢@RequestMapping)
(4)HandlerAdapter會根據Handler來呼叫真正的處理器開處理請求,並處理相應的業務邏輯。(處理方法)
(5)處理器處理完業務後,會返回一個ModelAndView物件,Model是返回的資料物件,View是個邏輯上的View。(返回處理結果)
(6)ViewResolver會根據邏輯View查詢實際的View。(邏輯檢視解析為真正的檢視)
(7)DispatcherServlet把返回的Model傳給View。(DispatcherServlet試圖渲染)
(8)通過View返回給請求者(瀏覽器)
四、DispatcherServlet詳細解析
原始碼分析:
package org.springframework.web.servlet; @SuppressWarnings("serial") public class DispatcherServlet extends FrameworkServlet { public static final String MULTIPART_RESOLVER_BEAN_NAME = "multipartResolver"; public static final String LOCALE_RESOLVER_BEAN_NAME = "localeResolver"; public static final String THEME_RESOLVER_BEAN_NAME = "themeResolver"; public static final String HANDLER_MAPPING_BEAN_NAME = "handlerMapping"; public static final String HANDLER_ADAPTER_BEAN_NAME = "handlerAdapter"; public static final String HANDLER_EXCEPTION_RESOLVER_BEAN_NAME = "handlerExceptionResolver"; public static final String REQUEST_TO_VIEW_NAME_TRANSLATOR_BEAN_NAME = "viewNameTranslator"; public static final String VIEW_RESOLVER_BEAN_NAME = "viewResolver"; public static final String FLASH_MAP_MANAGER_BEAN_NAME = "flashMapManager"; public static final String WEB_APPLICATION_CONTEXT_ATTRIBUTE = DispatcherServlet.class.getName() + ".CONTEXT"; public static final String LOCALE_RESOLVER_ATTRIBUTE = DispatcherServlet.class.getName() + ".LOCALE_RESOLVER"; public static final String THEME_RESOLVER_ATTRIBUTE = DispatcherServlet.class.getName() + ".THEME_RESOLVER"; public static final String THEME_SOURCE_ATTRIBUTE = DispatcherServlet.class.getName() + ".THEME_SOURCE"; public static final String INPUT_FLASH_MAP_ATTRIBUTE = DispatcherServlet.class.getName() + ".INPUT_FLASH_MAP"; public static final String OUTPUT_FLASH_MAP_ATTRIBUTE = DispatcherServlet.class.getName() + ".OUTPUT_FLASH_MAP"; public static final String FLASH_MAP_MANAGER_ATTRIBUTE = DispatcherServlet.class.getName() + ".FLASH_MAP_MANAGER"; public static final String EXCEPTION_ATTRIBUTE = DispatcherServlet.class.getName() + ".EXCEPTION"; public static final String PAGE_NOT_FOUND_LOG_CATEGORY = "org.springframework.web.servlet.PageNotFound"; private static final String DEFAULT_STRATEGIES_PATH = "DispatcherServlet.properties"; protected static final Log pageNotFoundLogger = LogFactory.getLog(PAGE_NOT_FOUND_LOG_CATEGORY); private static final Properties defaultStrategies; static { try { ClassPathResource resource = new ClassPathResource(DEFAULT_STRATEGIES_PATH, DispatcherServlet.class); defaultStrategies = PropertiesLoaderUtils.loadProperties(resource); } catch (IOException ex) { throw new IllegalStateException("Could not load 'DispatcherServlet.properties': " + ex.getMessage()); } } /** Detect all HandlerMappings or just expect "handlerMapping" bean? */ private boolean detectAllHandlerMappings = true; /** Detect all HandlerAdapters or just expect "handlerAdapter" bean? */ private boolean detectAllHandlerAdapters = true; /** Detect all HandlerExceptionResolvers or just expect "handlerExceptionResolver" bean? */ private boolean detectAllHandlerExceptionResolvers = true; /** Detect all ViewResolvers or just expect "viewResolver" bean? */ private boolean detectAllViewResolvers = true; /** Throw a NoHandlerFoundException if no Handler was found to process this request? **/ private boolean throwExceptionIfNoHandlerFound = false; /** Perform cleanup of request attributes after include request? */ private boolean cleanupAfterInclude = true; /** MultipartResolver used by this servlet */ private MultipartResolver multipartResolver; /** LocaleResolver used by this servlet */ private LocaleResolver localeResolver; /** ThemeResolver used by this servlet */ private ThemeResolver themeResolver; /** List of HandlerMappings used by this servlet */ private List<HandlerMapping> handlerMappings; /** List of HandlerAdapters used by this servlet */ private List<HandlerAdapter> handlerAdapters; /** List of HandlerExceptionResolvers used by this servlet */ private List<HandlerExceptionResolver> handlerExceptionResolvers; /** RequestToViewNameTranslator used by this servlet */ private RequestToViewNameTranslator viewNameTranslator; private FlashMapManager flashMapManager; /** List of ViewResolvers used by this servlet */ private List<ViewResolver> viewResolvers; public DispatcherServlet() { super(); } public DispatcherServlet(WebApplicationContext webApplicationContext) { super(webApplicationContext); } @Override protected void onRefresh(ApplicationContext context) { initStrategies(context); } protected void initStrategies(ApplicationContext context) { initMultipartResolver(context); initLocaleResolver(context); initThemeResolver(context); initHandlerMappings(context); initHandlerAdapters(context); initHandlerExceptionResolvers(context); initRequestToViewNameTranslator(context); initViewResolvers(context); initFlashMapManager(context); } }
DispatcherServlet類中的屬性beans:
1、HandlerMapping:
用於handlers對映請求和一系列的對於攔截器的前處理和後處理,大部分用@Controller註解。
HandlerMapping介面的實現類:
SimpleUrlHandlerMapping類通過配置檔案把URL對映到Controller類。
DefaultAnnotationHandlerMapping類通過註解把URL對映到Controller類。
2、HandlerAdapter:
幫助DispatcherServlet處理對映請求處理程式的介面卡,而不用考慮實際呼叫的是 哪個處理程式。
AnnotationMethodHandlerAdapter:通過註解,把請求URL對映到Controller類的方法上。
3、HandlerExceptionResolver:
處理對映異常。
SimpleMappingExceptionResolver通過配置檔案進行異常處理。
AnnotationMethodHandlerExceptionResolver:通過註解進行異常處理。
4、ViewResolver:
根據實際配置解析實際的View型別。
UrlBasedViewResolver類 通過配置檔案,把一個檢視名交給到一個View來處理。
5、LocaleResolver:
解決客戶正在使用的區域設定以及可能的時區,以便能夠提供國際化視野。
6、ThemeResolver:
解決Web應用程式可以使用的主題,例如提供個性化佈局。
7、MultipartResolver:
解析多部分請求,以支援從HTML表單上傳檔案。
8、FlashMapManager:
儲存並檢索可用於將一個請求屬性傳遞到另一個請求的input和output的FlashMap,通常用於重定向。