zoukankan      html  css  js  c++  java
  • SpringMvc详解

    1,整体架构图

    运行步骤:

    第一步:发起请求到前端控制器(DispatcherServlet)

    第二步:前端控制器请求HandlerMapping查找 Handler

             可以根据xml配置、注解进行查找

    第三步:处理器映射器HandlerMapping向前端控制器返回Handler

    第四步:前端控制器调用处理器适配器去执行Handler

    第五步:处理器适配器去执行Handler

    第六步:Handler执行完成给适配器返回ModelAndView

    第七步:处理器适配器向前端控制器返回ModelAndView

             ModelAndView是springmvc框架的一个底层对象,包括Model和view

    第八步:前端控制器请求视图解析器去进行视图解析

             根据逻辑视图名解析成真正的视图(jsp)

    第九步:视图解析器向前端控制器返回View

    第十步:前端控制器进行视图渲染

             视图渲染将模型数据(在ModelAndView对象中)填充到request域

    第十一步:前端控制器向用户响应结果

     
    2,源码解析

    DispatcherServlet.doDispatch 

    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 {
                     //检查是否是请求是否是multipart(如文件上传),如果是将通过MultipartResolver解析 processedRequest = checkMultipart(request); multipartRequestParsed = processedRequest != request; // 请求到处理器(页面控制器)的映射,通过HandlerMapping进行映射  mappedHandler = getHandler(processedRequest); if (mappedHandler == null || mappedHandler.getHandler() == null) { noHandlerFound(processedRequest, response); return; } // 处理器适配,即将我们的处理器包装成相应的适配器(从而支持多种类型的处理器) 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; } }                    //处理拦截器 preHandle         if (!mappedHandler.applyPreHandle(processedRequest, response)) { return; } try { // 由适配器执行处理器(调用处理器相应功能处理方法) mv = ha.handle(processedRequest, response, mappedHandler.getHandler()); } finally { if (asyncManager.isConcurrentHandlingStarted()) { return; } } applyDefaultViewName(request, mv)
                       //拦截器 postHandle                                                    mappedHandler.applyPostHandle(processedRequest, response, mv); } catch (Exception ex) { dispatchException = ex; }
                  //拦截器 afterHandle 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); } } }
     
    3,类结构图

    4,核心类介绍

    HandlerExecutionChain,由HandlerMethod和Interceptor集合组成的类,通过HandlerMapping接口的getHandler方法获取。
     
    public class HandlerExecutionChain {
    
    	private static final Log logger = LogFactory.getLog(HandlerExecutionChain.class);
    
    	private final Object handler;
    
    	private HandlerInterceptor[] interceptors;
    
    	private List<HandlerInterceptor> interceptorList;
    
    	private int interceptorIndex = -1;
    

    AbstractHandlerMapping,HandlerMapping的基础抽象类。  

    public abstract class AbstractHandlerMapping extends WebApplicationObjectSupport
    		implements HandlerMapping, Ordered {
    pu    public final HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
    		Object handler = getHandlerInternal(request);
    		if (handler == null) {
    			handler = getDefaultHandler();
    		}
    		if (handler == null) {
    			return null;
    		}
    		// Bean name or resolved handler?
    		if (handler instanceof String) {
    			String handlerName = (String) handler;
    			handler = getApplicationContext().getBean(handlerName);
    		}
    		return getHandlerExecutionChain(handler, request);
    	}
    }
    

      

  • 相关阅读:
    javaScript删除对象、数组中的null、undefined、空对象、空数组方法
    js数组方法 改变原数组和不改变原数组的方法整理
    js时间戳与日期格式的相互转换
    [原创]jquery更换头像
    css样式大全(copy自一个大佬的博文)
    【原创】实现点击按钮更换表格皮肤效果
    Cookie和Seesion
    常用正则表达式
    【原创】javaWeb完成增删查改功能
    javaWeb完成注册功能
  • 原文地址:https://www.cnblogs.com/qunan/p/7522459.html
Copyright © 2011-2022 走看看