• springMVC源码笔记


    springMVC 设计总览

    下图来源:https://www.cnblogs.com/fangjian0423/p/springMVC-directory-summary.html

    下图来源:https://www.cnblogs.com/sunniest/p/4555801.html

    核心类DispatcherServlet

    接收请求DispatcherServlet也是一个Servlet

        protected void doService(HttpServletRequest request, HttpServletResponse response) throws Exception {
           ...
            try {
                this.doDispatch(request, response);
            } finally {
                if (!WebAsyncUtils.getAsyncManager(request).isConcurrentHandlingStarted() && attributesSnapshot != null) {
                    this.restoreAttributesAfterInclude(request, attributesSnapshot);
                }
    
            }
    
        }
    
        protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
                    ...
                    mappedHandler = this.getHandler(processedRequest);
                    HandlerAdapter ha = this.getHandlerAdapter(mappedHandler.getHandler());
                    ...
        
                    mappedHandler.applyPreHandle(processedRequest, response)
                    mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
                    ...
                    this.applyDefaultViewName(request, mv);
                    mappedHandler.applyPostHandle(processedRequest, response, mv);
    
               
                    this.processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);
           
        }
    
    

    HandlerAdapter是一个适配器,根据handler适配ModelAndView

    getHandler(processedRequest)是为了获取HandlerExecutionChain:protected HandlerExecutionChain getHandler(HttpServletRequest request)

    HandlerExecutionChain 有什么:

        private static final Log logger = LogFactory.getLog(HandlerExecutionChain.class);
        private final Object handler;
        private HandlerInterceptor[] interceptors;
        private List<HandlerInterceptor> interceptorList;
        private int interceptorIndex;
    
        boolean applyPreHandle(HttpServletRequest request, HttpServletResponse response) throws Exception {
            if (this.getInterceptors() != null) {
                for(int i = 0; i < this.getInterceptors().length; this.interceptorIndex = i++) {
                    HandlerInterceptor interceptor = this.getInterceptors()[i];
                    if (!interceptor.preHandle(request, response, this.handler)) {
                        this.triggerAfterCompletion(request, response, (Exception)null);
                        return false;
                    }
                }
            }
    
            return true;
        }
    
        void applyPostHandle(HttpServletRequest request, HttpServletResponse response, ModelAndView mv) throws Exception {
            if (this.getInterceptors() != null) {
                for(int i = this.getInterceptors().length - 1; i >= 0; --i) {
                    HandlerInterceptor interceptor = this.getInterceptors()[i];
                    interceptor.postHandle(request, response, this.handler, mv);
                }
    
            }
        }
    
    

    HandlerInterceptor:拦截器(在handler处理前后执行applyPreHandle,applyPostHandle方法)
    handler:处理器

  • 相关阅读:
    Docker部署
    编写一个脚本用户进入容器
    Shell脚本写的《俄罗斯方块》
    Linux磁盘分区(9)
    Linux任务调度(8)
    Linux权限管理(7)
    Linux组管理(6)
    Linux实用指令(5)
    C#中 char、byte、string
    编码转换
  • 原文地址:https://www.cnblogs.com/lanqie/p/9544636.html
Copyright © 2020-2023  润新知