1. 程式人生 > >第四十章:Spring MVC框架之執行原理12

第四十章:Spring MVC框架之執行原理12

第十章 SpringMVC執行原理
找到一篇寫的不錯的部落格,大家可以看看
第一節 幾個重要元件
1.HandlerMapping

代表請求地址到handler之間的對映。
2.HandlerExecutionChain

handler的執行鏈物件,由handler物件和所有handler攔截器組成。SpringMVC呼叫HandlerMapping介面中定義的getHandler()方法獲取該物件。
3.HandlerAdapter

執行請求引數注入、型別轉換、資料驗證等具體操作。
第二節 關鍵節點
1.獲取HandlerExecutionChain物件

所在API:org.springframework.web.servlet.DispatcherServlet 原始碼位置:1101行、916行 Tips:如果當前請求沒有經過對映,那麼mappedHandler是否為null呢? ①如果配置了mvc:default-servlet-handler則不為null ②如果沒有配置mvc:default-servlet-handler則為null
2.獲取HandlerAdapter物件

所在API:org.springframework.web.servlet.DispatcherServlet 原始碼位置:923行
3.呼叫攔截器的preHandle()方法

所在API:org.springframework.web.servlet.DispatcherServlet 原始碼位置:939行
4.為模型物件注入請求引數

所在API:org.springframework.web.bind.annotation.support.HandlerMethodInvoker 原始碼位置:170行、373行
5.呼叫目標handler方法

所在API:org.springframework.web.servlet.DispatcherServlet 原始碼位置:945行
6.呼叫攔截器的postHandle()方法

所在API:org.springframework.web.servlet.DispatcherServlet 原始碼位置:954行
7.處理檢視轉發相關

所在API:org.springframework.web.servlet.DispatcherServlet 原始碼位置:959行
8.處理異常

所在API:org.springframework.web.servlet.DispatcherServlet 原始碼位置:998行
9.渲染檢視

所在API:org.springframework.web.servlet.DispatcherServlet 原始碼位置:1012行
①解析檢視名稱,將邏輯檢視轉換為物理檢視

所在API:org.springframework.web.servlet.DispatcherServlet 原始碼位置:1204行、1266行
②渲染檢視

所在API:org.springframework.web.servlet.DispatcherServlet 原始碼位置:1225行
③將模型資料暴露到請求域

所在API:org.springframework.web.servlet.view.AbstractView 原始碼位置:266行
④將模型資料儲存到請求域

所在API:org.springframework.web.servlet.view.AbstractView 原始碼位置:374行
⑤轉發

所在API:org.springframework.web.servlet.view.InternalResourceView 原始碼位置:209行
10.呼叫攔截器的afterCompletion方法

所在API:org.springframework.web.servlet.DispatcherServlet 原始碼位置:1030行
第三節 annotation相關

我們在前面的操作中發現,使用了mvc:default-servlet-handler和mvc:view-controller後必須使用mvc:annotation-driven。那麼這是為什麼呢?關鍵原因是他們載入使用的HandlerMapping不同。
1.三個都沒有使用時有效的HandlerMapping

org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping
org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping

其中DefaultAnnotationHandlerMapping負責把所有handler類中的handler方法收集起來。
2.增加了mvc:default-servlet-handler或mvc:view-controller後有效的HandlerMapping

org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping
org.springframework.web.servlet.handler.SimpleUrlHandlerMapping

很明顯,DefaultAnnotationHandlerMapping沒了,而SimpleUrlHandlerMapping只能對映靜態資源。所以我們通過@RequestMapping對映的handler方法無效了。
3.再增加了mvc:annotation-driven後

org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping 
org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping
org.springframework.web.servlet.handler.SimpleUrlHandlerMapping

加入了mvc:annotation-driven後最關鍵的是增加了RequestMappingHandlerMapping,從而可以對映我們的handler方法。

案例:
index.jsp:

<a href="${pageContext.request.contextPath }/test/work/flow">Test work flow</a>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<!-- The front controller of this Spring Web application, responsible for 
		handling all application requests -->
	<servlet>
		<servlet-name>springDispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring-mvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<!-- Map all requests to the DispatcherServlet for handling -->
	<servlet-mapping>
		<servlet-name>springDispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
	
	<context:component-scan base-package="com.atguigu.spring.mvc.handlers"/>
	
	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/page/"/>
		<property name="suffix" value=".jsp"/>
	</bean>
	
	<mvc:annotation-driven/>
	
	<mvc:interceptors>
		<mvc:interceptor>
			<mvc:mapping path="/**"/>
			<bean class="com.atguigu.spring.mvc.interceptor.WorkFlowInterceptor"/>
		</mvc:interceptor>
	</mvc:interceptors>

</beans>

interceptor

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class WorkFlowInterceptor implements HandlerInterceptor {

	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		System.out.println("my after completion");
	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		System.out.println("my post handle");
	}

	@Override
	public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
		System.out.println("my pre handle");
		return true;
	}

}

handlers

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class WorkFlowHandler {
	
	@RequestMapping("/test/work/flow")
	public String myHandle(Model model) {
		System.err.println("my handler method");
		model.addAttribute("message", "i love you!!!");
		return "target";
	}

}

結果:

資訊: Server startup in 33416 ms
my pre handle
my handler method
my post handle
my after completion

頁面結果:

Target
i love you!!!