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

欢迎留言交流:)

推荐阅读更多精彩内容