1. 程式人生 > >SpringMVC工作原理解讀

SpringMVC工作原理解讀

The Spring Web model-view-controller (MVC) framework is designed around aDispatcherServlet that dispatches requests to handlers, with configurable handlermappings, view resolution, locale, time zone and theme resolution as well as support foruploading files. The default handler is based on the @Controller

and @RequestMappingannotations, offering a wide range of flexible handling methods. With the introductionof Spring 3.0, the @Controller mechanism also allows you to create RESTful Web sitesand applications, through the @PathVariable annotation and other features.

SpringWeb MVC框架是圍繞著DispatcherServlet來設計的,其中DispatcherServlet分發請求給處理器,SpringMVC具有可配置的對映、檢視解析、區域設定、時區設定和主題解析以及上傳圖片檔案的支援等特性。預設的處理器基於@Controller註解和@RequestMapping註解,它提供了靈活的處理方法。在Spring3.0中,@Controller機制結合@PathVariable註解以及其他的特徵使得他允許建立支援RESTful風格的Web站點和應用。

下面的圖給出了其執行的主要流程


訪問的流程解析


基本步驟:

1. Http請求提交到Tomcat後,讀取web.xml中的配置,將請求交給DispatcherServlet處理.

2. DispatcherServlet根據Spring容器中的HandlerMapping查詢處理器。

3. 呼叫處理器,完成業務方法,返回ModelAndView

4. 通過檢視解析器查詢到ModelAndView所代表的檢視。

5. 將資料以Model的方式,填充到上一步中獲取到的檢視中去,完成整個的檢視的構造。

6. 將得到的檢視整體返回給請求方,通過web瀏覽器解析後呈現出來。