Spring思維導圖,讓Spring不再難懂

spring mvc簡介與運行原理 

Spring的模型-視圖-控制器(MVC)框架是圍繞一個DispatcherServlet來設計的,這個Servlet會把請求分發給各個處理器,並支持可配置的處理器映射、視圖渲染、本地化、時區與主題渲染等,甚至還能支持文件上傳。 

  • (1) Http請求:客戶端請求提交到DispatcherServlet。
  • (2) 尋找處理器:由DispatcherServlet控制器查詢一個或多個HandlerMapping,找到處理請求的Controller。
  • (3) 調用處理器:DispatcherServlet將請求提交到Controller。
  • (4)(5)調用業務處理和返回結果:Controller調用業務邏輯處理後,返回ModelAndView。
  • (6)(7)處理視圖映射並返回模型: DispatcherServlet查詢一個或多個ViewResoler視圖解析器,找到ModelAndView指定的視圖。
  • (8) Http響應:視圖負責將結果顯示到客戶端。
主要註解 

ContextLoaderListener 

在講ContextLoaderListener之前,首先來了解一下web.xml的作用。 
  • 一個web中可以沒有web.xml文件,也就是說,web.xml文件並不是web工程必須的。web.xml文件是用來初始化配置信息:比如Welcome頁面、servlet、servlet-mapping、filter、listener、啓動加載級別等。當你的web工程沒用到這些時,你可以不用web.xml文件來配置你的Application。
  • 當要啓動某個web項目時,服務器軟件或容器如(tomcat)會第一步加載項目中的web.xml文件,通過其中的各種配置來啓動項目,只有其中配置的各項均無誤時,項目才能正確啓動。web.xml有多項標籤,在其加載的過程中順序依次爲:context-param >> listener >> fileter >> servlet。(同類多個節點以出現順序依次加載)

而spring mvc啓動過程大致分爲兩個過程: 
  • ContextLoaderListener初始化,實例化IoC容器,並將此容器實例註冊到ServletContext中。
  • DispatcherServlet初始化。

其中ContextLoaderListener監聽器它實現了ServletContextListener這個接口,在web.xml配置這個監聽器,啓動容器時,就會默認執行它實現的方法。在ContextLoaderListener中關聯了ContextLoader這個類,所以整個加載配置過程由ContextLoader來完成。 

ContextLoaderListener在web.xml中的配置 
Java代碼 
  1. <!-- 配置contextConfigLocation初始化參數 -->  
  2.   
  3. <context-param>  
  4.     <param-name>contextConfigLocation</param-name>  
  5.   
  6.     <param-value>/WEB-INF/applicationContext.xml</param-value>  
  7.   
  8. </context-param>  
  9.   
  10. <!-- 配置ContextLoaderListerner -->  
  11.   
  12. <listener>  
  13.   
  14.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  15.   
  16. </listener>  

ServletContextListener 接口有兩個方法:contextInitialized,contextDestroyed 

DispatcherServlet 

Spring MVC框架,與其他很多web的MVC框架一樣:請求驅動;所有設計都圍繞着一箇中央Servlet來展開,它負責把所有請求分發到控制器;同時提供其他web應用開發所需要的功能。不過Spring的中央處理器,DispatcherServlet,能做的比這更多。 

下圖展示了Spring Web MVC的DispatcherServlet處理請求的工作流。熟悉設計模式的朋友會發現,DispatcherServlet應用的其實就是一個“前端控制器”的設計模式(其他很多優秀的web框架也都使用了這個設計模式)。 

流程圖 

spring mvc處理請求的流程.jpg

在web.xml中的配置 
Java代碼 
  1. <!-- servlet定義 -->  
  2.   
  3. <servlet>  
  4.     <servlet-name>dispatcher</servlet-name>  
  5.     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  6.   
  7.     <load-on-startup>1</load-on-startup>  
  8.   
  9. </servlet>  
  10.   
  11. <servlet-mapping>  
  12.     <servlet-name>dispatcher</servlet-name>  
  13.   
  14.     <url-pattern>/</url-pattern>  
  15.   
  16. </servlet-mapping>  

其中 
  • load-on-startup:表示啓動容器時初始化該Servlet;
  • url-pattern:表示哪些請求交給Spring Web MVC處理, “/” 是用來定義默認servlet映射的。也可以如“*.html”表示攔截所有以html爲擴展名的請求。
在Spring MVC中,每個DispatcherServlet都持有一個自己的上下文對象WebApplicationContext,它又繼承了根(root)WebApplicationContext對象中已經定義的所有bean。這些繼承的bean可以在具體的Servlet實例中被重載,在每個Servlet實例中你也可以定義其scope下的新bean。 

WebApplicationContext繼承自ApplicationContext,它提供了一些web應用經常需要用到的特性。它與普通的ApplicationContext不同的地方在於,它支持主題的解析,並且知道它關聯到的是哪個servlet(它持有一個該ServletContext的引用) 

spring mvc同時提供了很多特殊的註解,用於處理請求和渲染視圖等。DispatcherServlet初始化的過程中會默認使用這些特殊bean進行配置。如果你想指定使用哪個特定的bean,你可以在web應用上下文WebApplicationContext中簡單地配置它們。 

其中,常用的ViewResolver的配置。以jsp作爲視圖爲例 
Java代碼 
  1. <!-- 對模型視圖名稱的解析,即在模型視圖名稱添加前後綴 -->  
  2.   
  3. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  4.     <property name="prefix" value="/WEB-INF/jsp/" />  
  5.   
  6.     <property name="suffix" value=".jsp" />  
  7.   
  8. </bean>  

配置上傳文件限制MultipartResolver 
Java代碼 
  1. <!-- 上傳限制 -->  
  2.   
  3. <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
  4.      <!-- 上傳文件大小限制爲31M,31*1024*1024 -->  
  5.   
  6.      <property name="maxUploadSize" value="32505856"/>  
  7.   
  8. </bean>  

applicationContext.xml中的標籤 

文件上傳 

前面說到DispatcherServlet中有個特殊的Bean叫MultipartResolver,可用於限制文件的上傳大小等。當解析器MultipartResolver完成處理時,請求便會像其他請求一樣被正常流程處理。 

表單 
Java代碼 
  1. <form method="post" action="/form" enctype="multipart/form-data">  
  2.      <input type="text" name="name"/>  
  3.      <input type="file" name="file"/>  
  4.      <input type="submit"/>  
  5. </form>  

控制器 
Java代碼 
  1. @RequestMapping(path = "/form", method = RequestMethod.POST) public String handleFormUpload(@RequestParam("name") String name,   
  2.   
  3.             @RequestParam("file") MultipartFile file) {  
  4.   
  5.    if (!file.isEmpty()) {  
  6.   
  7.           byte[] bytes = file.getBytes();  
  8.   
  9.           // store the bytes somewhere  
  10.           return "redirect:uploadSuccess";  
  11.   
  12.     }  
  13.   
  14.     return "redirect:uploadFailure";  
  15. }  

異常處理 

先來說下常見的異常處理有幾種方式,如下圖: 

Spring的處理器異常解析器HandlerExceptionResolver接口的實現負責處理各類控制器執行過程中出現的異常。也是上面提到的,是DispatcherServlet中的特殊bean,可以自定義配置處理。 

某種程度上講,HandlerExceptionResolver與你在web應用描述符web.xml文件中能定義的異常映射(exception mapping)很相像,不過它比後者提供了更靈活的方式。比如它能提供異常被拋出時正在執行的是哪個處理器這樣的信息。 

HandlerExceptionResolver 提供resolveException接口 
Java代碼 
  1. public interface HandlerExceptionResolver {    
  2.     ModelAndView resolveException(    
  3.             HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex);    
  4. }  

在BaseController中使用 @ExceptionHandler註解處理異常 
Java代碼 
  1. @ExceptionHandler(Exception.class)  
  2. public Object exceptionHandler(Exception ex, HttpServletResponse response,   
  3.           HttpServletRequest request) throws IOException {  
  4.     String url = "";  
  5.     String msg = ex.getMessage();  
  6.     Object resultModel = null;  
  7.     try {  
  8.         if (ex.getClass() == HttpRequestMethodNotSupportedException.class) {  
  9.             url = "admin/common/500";  
  10.             System.out.println("--------毛有找到對應方法---------");  
  11.         } else if (ex.getClass() == ParameterException.class) {//自定義的異常  
  12.   
  13.         } else if (ex.getClass() == UnauthorizedException.class) {  
  14.             url = "admin/common/unauth";  
  15.             System.out.println("--------毛有權限---------");  
  16.         }  
  17.   
  18.         String header = req.getHeader("X-Requested-With");  
  19.         boolean isAjax = "XMLHttpRequest".equalsIgnoreCase(header);  
  20.         String method = req.getMethod();  
  21.         boolean isPost = "POST".equalsIgnoreCase(method);  
  22.   
  23.         if (isAjax || isPost) {  
  24.             return Message.error(msg);  
  25.         } else {  
  26.             ModelAndView view = new ModelAndView(url);  
  27.             view.addObject("error", msg);  
  28.             view.addObject("class", ex.getClass());  
  29.             view.addObject("method", request.getRequestURI());  
  30.             return view;  
  31.         }  
  32.     } catch (Exception exception) {  
  33.         logger.error(exception.getMessage(), exception);  
  34.         return resultModel;  
  35.     } finally {  
  36.         logger.error(msg, ex);  
  37.         ex.printStackTrace();  
  38.     }  
  39. }  

在web.xml中處理異常 
Java代碼 
  1. <!-- 默認的錯誤處理頁面 -->  
  2.   
  3. <error-page>  
  4.     <error-code>403</error-code>  
  5.   
  6.     <location>/403.html</location>  
  7.   
  8. </error-page>  
  9.   
  10. <error-page>  
  11.     <error-code>404</error-code>  
  12.   
  13.     <location>/404.html</location>  
  14.   
  15. </error-page>  
  16.   
  17. <!-- 僅僅在調試的時候注視掉,在正式部署的時候不能註釋 --><!-- 這樣配置也是可以的,表示發生500錯誤的時候,轉到500.jsp頁面處理。 -->  
  18.   
  19. <error-page>   
  20.     <error-code>500</error-code>   
  21.   
  22.     <location>/500.html</location>   
  23.   
  24. </error-page>   
  25.   
  26. <!-- 這樣的配置表示如果jsp頁面或者servlet發生java.lang.Exception類型(當然包含子類)的異常就會轉到500.jsp頁面處理。 -->  
  27.   
  28. <error-page>   
  29.     <exception-type>java.lang.Exception</exception-type>   
  30.   
  31.     <location>/500.jsp</location>   
  32.   
  33. </error-page>   
  34.   
  35. <error-page>   
  36.     <exception-type>java.lang.Throwable</exception-type>   
  37.   
  38.     <location>/500.jsp</location>   
  39.   
  40. </error-page>  
  41.   
  42. <!-- 當error-code和exception-type都配置時,exception-type配置的頁面優先級高及出現500錯誤,發生異常Exception時會跳轉到500.jsp-->  

來一個問題:HandlerExceptionResolver和web.xml中配置的error-page會有衝突嗎? 

解答:如果resolveException返回了ModelAndView,會優先根據返回值中的頁面來顯示。不過,resolveException可以返回null,此時則展示web.xml中的error-page的500狀態碼配置的頁面。 
當web.xml中有相應的error-page配置,則可以在實現resolveException方法時返回null。 
API文檔中對返回值的解釋: 
return a corresponding ModelAndView to forward to, or null for default processing. 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章