Spring MVC介紹(二)之 Spring MVC 執(zhí)行流程解析
一、MVC組件執(zhí)行流程
回顧一下MVC的體系結(jié)構(gòu)與組件執(zhí)行流程佃却,如下圖:
dispatcherServlet -> handlermapping ->基于url查找handler -> handlerAdapter
-> 基于handler找到adapter -> 由adapter找到我們的 handler -> 執(zhí)行業(yè)務(wù)處理返回 modelAndView
-> viewResolver -> 基于viweName找到view -> 執(zhí)行視圖解析 -> 返回前端
再來看一個(gè)例子者吁,基于beanNameHandlerMapper的示例:
BeanNameControl.java
package com.demo.spring.mvc.control;
import org.springframework.web.HttpRequestHandler;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* com.demo.spring.mvc.control
*
* @author Zyy
* @date 2019/2/24 15:34
*/
public class BeanNameControl implements HttpRequestHandler {
@Override
public void handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
httpServletResponse.getWriter().println("BeanName Control");
}
}
spring-mvc_beanName.xml
<?xml version="1.0" encoding="GBK"?>
<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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<bean id="/beanName" class="com.demo.spring.mvc.control.BeanNameControl"></bean>
</beans>
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" xmlns:web="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>spring-mvc</display-name>
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:/spring-mvc_beanName.xml
</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
</web-app>
此時(shí)啟動(dòng)容器,訪問 http://localhost:8080/spring_mvc/beanName 双霍,可以看到顯示的是我們control返回的數(shù)據(jù)砚偶。
BeanName Control
我們在此之前未配置HandlerMapping批销、HandlerAdapter、InternalResourceViewResolver染坯,為何還是能找到呢均芽?原因是因?yàn)閟pring有個(gè)默認(rèn)的配置:
路徑如下:
org\springframework\spring-webmvc\4.3.8.RELEASE\spring-webmvc-4.3.8.RELEASE.jar!\org\springframework\web\servlet\DispatcherServlet.properties
DispatcherServlet.properties
# Default implementation classes for DispatcherServlet's strategy interfaces.
# Used as fallback when no matching beans are found in the DispatcherServlet context.
# Not meant to be customized by application developers.
org.springframework.web.servlet.LocaleResolver=org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver
org.springframework.web.servlet.ThemeResolver=org.springframework.web.servlet.theme.FixedThemeResolver
org.springframework.web.servlet.HandlerMapping=org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,\
org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping
org.springframework.web.servlet.HandlerAdapter=org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,\
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,\
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
org.springframework.web.servlet.HandlerExceptionResolver=org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver,\
org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver,\
org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver
org.springframework.web.servlet.RequestToViewNameTranslator=org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator
org.springframework.web.servlet.ViewResolver=org.springframework.web.servlet.view.InternalResourceViewResolver
org.springframework.web.servlet.FlashMapManager=org.springframework.web.servlet.support.SessionFlashMapManager
在這里面可以看到:
BeanNameUrlHandlerMapping
HttpRequestHandlerAdapter
InternalResourceViewResolver
由此我們知道,spring有一套默認(rèn)的mvc處理實(shí)現(xiàn)单鹿。
二掀宋、HandlerMapping
HandlerMapping為mvc中url路徑與control對(duì)象的映射,dispatcherServlet就是基于handlerMapping組件來尋找對(duì)應(yīng)的control仲锄,找不到時(shí)會(huì)拋出異常 noHandlerFound異常劲妙。
HandlerMapping.java
package org.springframework.web.servlet;
import javax.servlet.http.HttpServletRequest;
public interface HandlerMapping {
String PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE = HandlerMapping.class.getName() + ".pathWithinHandlerMapping";
String BEST_MATCHING_PATTERN_ATTRIBUTE = HandlerMapping.class.getName() + ".bestMatchingPattern";
String INTROSPECT_TYPE_LEVEL_MAPPING = HandlerMapping.class.getName() + ".introspectTypeLevelMapping";
String URI_TEMPLATE_VARIABLES_ATTRIBUTE = HandlerMapping.class.getName() + ".uriTemplateVariables";
String MATRIX_VARIABLES_ATTRIBUTE = HandlerMapping.class.getName() + ".matrixVariables";
String PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE = HandlerMapping.class.getName() + ".producibleMediaTypes";
HandlerExecutionChain getHandler(HttpServletRequest var1) throws Exception;
}
其實(shí)現(xiàn)類,如下:
目前大部分使用其中三種:
1儒喊、org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping:
基于bean name查找镣奋,在xml以“/”開頭
<bean id="/beanName" class="com.demo.spring.mvc.control.BeanNameControl"></bean>
2、org.springframework.web.servlet.handler.SimpleUrlHandlerMapping
手動(dòng)在xml中配置url與control映射
<bean id="simple" class="com.demo.spring.mvc.control.SimpleControl"></bean>
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap">
<props>
<prop key="/hello.do">simple</prop>
</props>
</property>
</bean>
3怀愧、org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping
基于@RequestMapping 注解侨颈,配置對(duì)應(yīng)映射
AnnotationControl.java
package com.demo.spring.mvc.control;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
* com.demo.spring.mvc.control
*
* @author Zyy
* @date 2019/2/24 16:35
*/
@Controller
@RequestMapping("/annotation")
public class AnnotationControl {
@RequestMapping("/")
public ModelAndView get() {
//mapped to localhost:8080/spring_mvc/annotation/
ModelAndView modelAndView = new ModelAndView("userView");
modelAndView.addObject("name","this is /annotation/");
return modelAndView;
}
@RequestMapping("/index")
public ModelAndView index() {
//mapped to localhost:8080/spring_mvc/annotation/index/
ModelAndView modelAndView = new ModelAndView("userView");
modelAndView.addObject("name","this is /annotation/index");
return modelAndView;
}
}
spring-mvc-annotation.xml
<?xml version="1.0" encoding="GBK"?>
<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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.demo.spring.mvc.control" />
<mvc:annotation-driven/>
<!-- 視圖倉庫 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/page/" />
<property name="suffix" value=".jsp" />
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
</bean>
</beans>
web.xml
classpath:/spring-mvc-annotation.xml
當(dāng)ioc中實(shí)例化這些類之后,dispatcherServlet就會(huì)通過org.springframework.web.servlet.DispatcherServlet#getHandler(HttpServletRequest request)方法基于request芯义,查找對(duì)應(yīng)的handler哈垢,這里就會(huì)用到的HandlerAdapter,根據(jù)handler的不用采用不同的handlerAdapter(適配器模式)扛拨。
三耘分、HandlerAdapter
Spring mvc采用適配器模式來適配調(diào)用指定的handler,根據(jù)handler的不同種類绑警,采用不同的Adapter求泰,其中handler與handlerAdapter對(duì)應(yīng)關(guān)系如下:
Handler類別 | HandlerAdapter | 描述 |
---|---|---|
Control | SimpleControllerHandlerAdapter | 標(biāo)準(zhǔn)控制器,返回ModelAndView |
HttpRequestHandler | HttpRequestHandlerAdapter | 業(yè)務(wù)自行處理請(qǐng)求待秃,不需要通過ModelAndView轉(zhuǎn)到視圖 |
Servlet | SimpleServletHandlerAdapter | 基于標(biāo)準(zhǔn)的Servlet處理 |
HandlerMethod | RequestMappingHandlerAdapter | 基于@RequestMapping對(duì)應(yīng)方法處理 |
HandlerAdapter.java
package org.springframework.web.servlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public interface HandlerAdapter {
boolean supports(Object var1);
ModelAndView handle(HttpServletRequest var1, HttpServletResponse var2, Object var3) throws Exception;
long getLastModified(HttpServletRequest var1, Object var2);
}
其中xml配置拜秧,例如:
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
當(dāng)ioc容器實(shí)例化配置的類之后,dispatcherServlet會(huì)通過org.springframework.web.servlet.DispatcherServlet#getHandlerAdapter來獲取HandlerAdapter章郁,如果找不到則拋出異常 No adapter for handler枉氮。
四、ViewResolver 與 View
找到對(duì)應(yīng)的Adapter之后暖庄,就會(huì)基于適配器調(diào)用業(yè)務(wù)處理聊替,處理完成之后,業(yè)務(wù)方法會(huì)返回一個(gè)ModelAndView培廓,接下來會(huì)去查找對(duì)應(yīng)的視圖進(jìn)行處理惹悄,dispatcherServlet會(huì)通過調(diào)用org.springframework.web.servlet.DispatcherServlet#resolveViewName,遍歷viewResolvers肩钠,然后根據(jù)viewName找到對(duì)應(yīng)的View泣港,如果找不到則拋出異常:
Could not resolve view with name暂殖。
ViewResolver viewResolver = (ViewResolver)var5.next();
view = viewResolver.resolveViewName(viewName, locale
ViewResolver.java
package org.springframework.web.servlet;
import java.util.Locale;
public interface ViewResolver {
View resolveViewName(String var1, Locale var2) throws Exception;
}
View.java
package org.springframework.web.servlet;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public interface View {
String RESPONSE_STATUS_ATTRIBUTE = View.class.getName() + ".responseStatus";
String PATH_VARIABLES = View.class.getName() + ".pathVariables";
String SELECTED_CONTENT_TYPE = View.class.getName() + ".selectedContentType";
String getContentType();
void render(Map<String, ?> var1, HttpServletRequest var2, HttpServletResponse var3) throws Exception;
}
ViewResolver調(diào)用resolveViewName方法來獲取對(duì)應(yīng)的view,然后解析生成對(duì)應(yīng)的html当纱,然后返回前臺(tái)呛每。
五、HandlerExceptionResolver
當(dāng)出現(xiàn)異常時(shí)坡氯,dispatcherServlet會(huì)調(diào)用org.springframework.web.servlet.DispatcherServlet#processHandlerException方法晨横,遍歷handlerExceptionResolvers,然后進(jìn)行異常處理箫柳,處理完成之后返回errorView跳轉(zhuǎn)到我們的異常視圖進(jìn)行顯示手形。
HandlerExceptionResolver.java
package org.springframework.web.servlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public interface HandlerExceptionResolver {
ModelAndView resolveException(HttpServletRequest var1, HttpServletResponse var2, Object var3, Exception var4);
}
示例代碼:
SimpleExceptionHandler.java
package com.demo.spring.mvc.control;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* com.demo.spring.mvc.control
*
* @author Zyy
* @date 2019/2/24 17:41
*/
public class SimpleExceptionHandler implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
return new ModelAndView("error");
}
}
spring-mvc.xml
<!-- 自定義異常處理器 -->
<bean class="com.demo.spring.mvc.control.SimpleExceptionHandler"/>
web.xml
classpath:/spring-mvc-interceptors.xml
六、HandlerInterceptor
除了異常處理之外悯恍,spring mvc還引入了攔截器interceptor機(jī)制库糠,類似Filter。
HandlerInterceptor.java
package org.springframework.web.servlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public interface HandlerInterceptor {
boolean preHandle(HttpServletRequest var1, HttpServletResponse var2, Object var3) throws Exception;
void postHandle(HttpServletRequest var1, HttpServletResponse var2, Object var3, ModelAndView var4) throws Exception;
void afterCompletion(HttpServletRequest var1, HttpServletResponse var2, Object var3, Exception var4) throws Exception;
}
示例代碼:
SimpleHandlerInterceptor.java
package com.demo.spring.mvc.control;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* com.demo.spring.mvc.control
*
* @author Zyy
* @date 2019/2/19 22:59
*/
public class SimpleHandlerInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
System.out.println("preHandle...");
return true;
}
@Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
System.out.println("postHandle...");
}
@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
System.out.println("afterCompletion...");
}
}
spring-mvc.xml
<?xml version="1.0" encoding="GBK"?>
<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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--控制器-->
<bean id="simpleControl" class="com.demo.spring.mvc.control.SimpleControl"/>
<bean id="handlerInterceptor" class="com.demo.spring.mvc.control.SimpleHandlerInterceptor"></bean>
<!-- url映謝器-->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap">
<props>
<prop key="/hello.do">simpleControl</prop>
</props>
</property>
<property name="interceptors" ref="handlerInterceptor"/>
</bean>
<!-- 執(zhí)行適配器-->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<!--資源解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/page/"/>
<property name="suffix" value=".jsp"/>
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
</bean>
<!-- 自定義異常處理器 -->
<bean class="com.demo.spring.mvc.control.SimpleExceptionHandler"/>
</beans>
其中調(diào)用過程是在HandlerExecutionChain中進(jìn)行調(diào)用以下方法:
- preHandle :業(yè)務(wù)處理前執(zhí)行
- postHandle :業(yè)務(wù)處理后執(zhí)行(異常則不執(zhí)行)
- afterCompletion : 視圖處理后執(zhí)行
整個(gè)的MVC 的執(zhí)行流程可以看下DispatcherServlet中的doDispatch方法涮毫。
org.springframework.web.servlet.DispatcherServlet#doDispatch
歡迎留言交流:)