Spring MVC介绍(二)之 Spring MVC 执行流程解析

spring.png

Spring MVC介绍(二)之 Spring MVC 执行流程解析

一、MVC组件执行流程

回顾一下MVC的体系结构与组件执行流程,如下图:

Spring MVC组件执行流程.png
dispatcherServlet -> handlermapping ->基于url查找handler -> handlerAdapter
-> 基于handler找到adapter  -> 由adapter找到我们的 handler -> 执行业务处理返回 modelAndView
-> viewResolver -> 基于viweName找到view -> 执行视图解析 -> 返回前端

再来看一个例子,基于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>

此时启动容器,访问 http://localhost:8080/spring_mvc/beanName ,可以看到显示的是我们control返回的数据。

BeanName Control

我们在此之前未配置HandlerMapping、HandlerAdapter、InternalResourceViewResolver,为何还是能找到呢?原因是因为spring有个默认的配置:

路径如下:

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有一套默认的mvc处理实现。

二、HandlerMapping

HandlerMapping为mvc中url路径与control对象的映射,dispatcherServlet就是基于handlerMapping组件来寻找对应的control,找不到时会抛出异常 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;
}

其实现类,如下:

handlerMapping实现类.png

目前大部分使用其中三种:

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

手动在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 注解,配置对应映射

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

当ioc中实例化这些类之后,dispatcherServlet就会通过org.springframework.web.servlet.DispatcherServlet#getHandler(HttpServletRequest request)方法基于request,查找对应的handler,这里就会用到的HandlerAdapter,根据handler的不用采用不同的handlerAdapter(适配器模式)。

三、HandlerAdapter

Spring mvc采用适配器模式来适配调用指定的handler,根据handler的不同种类,采用不同的Adapter,其中handler与handlerAdapter对应关系如下:

Handler类别 HandlerAdapter 描述
Control SimpleControllerHandlerAdapter 标准控制器,返回ModelAndView
HttpRequestHandler HttpRequestHandlerAdapter 业务自行处理请求,不需要通过ModelAndView转到视图
Servlet SimpleServletHandlerAdapter 基于标准的Servlet处理
HandlerMethod RequestMappingHandlerAdapter 基于@RequestMapping对应方法处理

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);
}
HandlerAdapter 接口结构图.png

其中xml配置,例如:

<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>

当ioc容器实例化配置的类之后,dispatcherServlet会通过org.springframework.web.servlet.DispatcherServlet#getHandlerAdapter来获取HandlerAdapter,如果找不到则抛出异常 No adapter for handler。

四、ViewResolver 与 View

找到对应的Adapter之后,就会基于适配器调用业务处理,处理完成之后,业务方法会返回一个ModelAndView,接下来会去查找对应的视图进行处理,dispatcherServlet会通过调用org.springframework.web.servlet.DispatcherServlet#resolveViewName,遍历viewResolvers,然后根据viewName找到对应的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;
}
ViewResolver 接口结构.png

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;
}
View 接口结构图.png

ViewResolver调用resolveViewName方法来获取对应的view,然后解析生成对应的html,然后返回前台。

五、HandlerExceptionResolver

当出现异常时,dispatcherServlet会调用org.springframework.web.servlet.DispatcherServlet#processHandlerException方法,遍历handlerExceptionResolvers,然后进行异常处理,处理完成之后返回errorView跳转到我们的异常视图进行显示。

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);
}
HandlerExceptionResovler 接口结构图.png

示例代码:

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机制,类似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;
}
HandlerInterceptor 接口结构图.png

示例代码:

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>

    <!-- 执行适配器-->
    <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>

其中调用过程是在HandlerExecutionChain中进行调用以下方法:

  • preHandle :业务处理前执行
  • postHandle :业务处理后执行(异常则不执行)
  • afterCompletion : 视图处理后执行

整个的MVC 的执行流程可以看下DispatcherServlet中的doDispatch方法。

org.springframework.web.servlet.DispatcherServlet#doDispatch

欢迎留言交流:)

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,271评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,275评论 2 380
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,151评论 0 336
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,550评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,553评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,559评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,924评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,580评论 0 257
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,826评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,578评论 2 320
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,661评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,363评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,940评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,926评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,156评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,872评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,391评论 2 342

推荐阅读更多精彩内容