Spring MVC框架:第十二章:運(yùn)行原理
SpringMVC運(yùn)行原理
找到一篇寫的不錯(cuò)的博客,大家可以看看
第一節(jié) 幾個(gè)重要組件
1.HandlerMapping
代表請(qǐng)求地址到handler之間的映射。
2.HandlerExecutionChain
handler的執(zhí)行鏈對(duì)象,由handler對(duì)象和所有handler攔截器組成。SpringMVC調(diào)用HandlerMapping接口中定義的getHandler()方法獲取該對(duì)象。
3.HandlerAdapter
執(zhí)行請(qǐng)求參數(shù)注入、類型轉(zhuǎn)換、數(shù)據(jù)驗(yàn)證等具體操作。
第二節(jié) 關(guān)鍵節(jié)點(diǎn)
1.獲取HandlerExecutionChain對(duì)象
所在API:org.springframework.web.servlet.DispatcherServlet
源碼位置:1101行、916行 Tips:如果當(dāng)前請(qǐng)求沒有經(jīng)過映射,那么mappedHandler是否為null呢?
①如果配置了mvc:default-servlet-handler則不為null
②如果沒有配置mvc:default-servlet-handler則為null
2.獲取HandlerAdapter對(duì)象
所在API:org.springframework.web.servlet.DispatcherServlet 源碼位置:923行
3.調(diào)用攔截器的preHandle()方法
所在API:org.springframework.web.servlet.DispatcherServlet 源碼位置:939行
4.為模型對(duì)象注入請(qǐng)求參數(shù)
所在API:org.springframework.web.bind.annotation.support.HandlerMethodInvoker 源碼位置:170行、373行
5.調(diào)用目標(biāo)handler方法
所在API:org.springframework.web.servlet.DispatcherServlet 源碼位置:945行
6.調(diào)用攔截器的postHandle()方法
所在API:org.springframework.web.servlet.DispatcherServlet 源碼位置:954行
7.處理視圖轉(zhuǎn)發(fā)相關(guān)
所在API:org.springframework.web.servlet.DispatcherServlet 源碼位置:959行
8.處理異常
所在API:org.springframework.web.servlet.DispatcherServlet 源碼位置:998行
9.渲染視圖
所在API:org.springframework.web.servlet.DispatcherServlet 源碼位置:1012行
①解析視圖名稱,將邏輯視圖轉(zhuǎn)換為物理視圖
所在API:org.springframework.web.servlet.DispatcherServlet 源碼位置:1204行、1266行
②渲染視圖
所在API:org.springframework.web.servlet.DispatcherServlet 源碼位置:1225行
③將模型數(shù)據(jù)暴露到請(qǐng)求域
所在API:org.springframework.web.servlet.view.AbstractView 源碼位置:266行
④將模型數(shù)據(jù)保存到請(qǐng)求域
所在API:org.springframework.web.servlet.view.AbstractView 源碼位置:374行
⑤轉(zhuǎn)發(fā)
所在API:org.springframework.web.servlet.view.InternalResourceView 源碼位置:209行
10.調(diào)用攔截器的afterCompletion方法
所在API:org.springframework.web.servlet.DispatcherServlet 源碼位置:1030行
第三節(jié) annotation相關(guān)
我們?cè)谇懊娴牟僮髦邪l(fā)現(xiàn),使用了mvc:default-servlet-handler和mvc:view-controller后必須使用mvc:annotation-driven。那么這是為什么呢?關(guān)鍵原因是他們加載使用的HandlerMapping不同。
1.三個(gè)都沒有使用時(shí)有效的HandlerMapping
org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping
org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping
其中DefaultAnnotationHandlerMapping負(fù)責(zé)把所有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只能映射靜態(tài)資源。所以我們通過@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后最關(guān)鍵的是增加了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";
}
}
結(jié)果:
信息: Server startup in 33416 ms
my pre handle
my handler method
my post handle
my after completion
頁面結(jié)果:
Target
i love you!!!