【2016-05-09】SpringMVC结构简介(half done)

前面写了篇文章Jersey vs. SpringMVC从使用角度简单写了下两者的区别,写完后发现对两个框架都不了解,于是搜集了SpringMVC的资料,本文为整理的内容。

一、SpringMVC中重要的概念:

  • DispatcherServlet

  • HandlerMapping:通过request找到handler的

  • HandlerAdapter

  • Controller

  • ViewResolver

  • View

 

二、Request处理流程

SpringMVC中最重要的类是DispatcherServlet,即前端控制器,负责处理request,生成response的整个过程。

DispatcherServlet本身是个Servlet,因此配在web.xml中,简单配置如下:

<servlet>
    <servlet-name>springWebMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>springWebMVC</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

Web容器启动项目的时候会加载DispatcherServlet,同时会在onRefresh方法中初始化了一系列HandlerMapping、HandlerAdapter、ViewResolver等bean。

处理Request的流程如下图(图片来自refer1)

【2016-05-09】SpringMVC结构简介(half done)_第1张图片

  • 调用HandlerMapping的getHandler方法找到对应的HandlerExecutionChain(Intecepter和Handler);

  • 找到了Handler,调用Handler的handle方法,处理request;

  • 将处理结果转换成对应的View,比如JSP、FreeMarker、PDF、Excel等。

doDispatch源码如下(Spring版本:3.2.8.RELEASE):

protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
    HttpServletRequest processedRequest = request;
    HandlerExecutionChain mappedHandler = null;
    boolean multipartRequestParsed = false;
    WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
    try {
        ModelAndView mv = null;
        Exception dispatchException = null;
        try {
            processedRequest = checkMultipart(request);
            multipartRequestParsed = processedRequest != request;
            // Determine handler for the current request.
            mappedHandler = getHandler(processedRequest, false);
            if (mappedHandler == null || mappedHandler.getHandler() == null) {
                noHandlerFound(processedRequest, response);
                return;
            }
            // Determine handler adapter for the current request.
            HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
            // Process last-modified header, if supported by the handler.
            String method = request.getMethod();
            boolean isGet = "GET".equals(method);
            if (isGet || "HEAD".equals(method)) {
                long lastModified = ha.getLastModified(request, mappedHandler.getHandler());
                if (logger.isDebugEnabled()) {
                    String requestUri = urlPathHelper.getRequestUri(request);
                    logger.debug("Last-Modified value for [" + requestUri + "] is: " + lastModified);
                }
                if (new ServletWebRequest(request, response).checkNotModified(lastModified) && isGet) {
                    return;
                }
            }
            if (!mappedHandler.applyPreHandle(processedRequest, response)) {
                return;
            }
            try {
                // Actually invoke the handler.
                mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
            }
            finally {
                if (asyncManager.isConcurrentHandlingStarted()) {
                    return;
                }
            }
            applyDefaultViewName(request, mv);
            mappedHandler.applyPostHandle(processedRequest, response, mv);
        }
        catch (Exception ex) {
            dispatchException = ex;
        }
        processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);
    }
    catch (Exception ex) {
        triggerAfterCompletion(processedRequest, response, mappedHandler, ex);
    }
    catch (Error err) {
        triggerAfterCompletionWithError(processedRequest, response, mappedHandler, err);
    }
    finally {
        if (asyncManager.isConcurrentHandlingStarted()) {
            // Instead of postHandle and afterCompletion
            mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response);
            return;
        }
        // Clean up any resources used by a multipart request.
        if (multipartRequestParsed) {
            cleanupMultipart(processedRequest);
        }
    }
}

@Controller:

 

 

@RequestMapping

http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.html

 

refer1:

1、Spring MVC 入门示例讲解:http://www.importnew.com/15141.html

你可能感兴趣的:(【2016-05-09】SpringMVC结构简介(half done))