• RequestMappingHandlerAdapter类简介说明


    转自:

    RequestMappingHandlerAdapter类简介说明

    下文笔者讲述RequestMappingHandlerAdapter类的简介说明,如下所示:

    RequestMappingHandlerAdapter类简介

    RequestMappingHandlerAdapter继承了AbstractHandlerMethodAdapter类
    它是一个HandlerAdapter接口的实现类
    它真真意义上实现了HandlerAdapter接口的类,

    下面将依次讲述其方法的功能

    supportsInternal() 默认返回 true

    protected boolean supportsInternal(HandlerMethod handlerMethod) {
        return true;
    }
    说明只要处理器是 HandlerMethod 类即可.
    

    handleInternal() 负责调用 HandlerMethod(处理器),并返回 ModelAndView

    protected ModelAndView handleInternal(HttpServletRequest request, HttpServletResponse response, 
        HandlerMethod handlerMethod) throws Exception {
        // 1.校验请求
        // 检查是否支持当前 rqeuest 的 method 和 session
        checkRequest(request);
    
        // 2.判断控制器是否存在 @SessionAttributes 注解
        if (getSessionAttributesHandler(handlerMethod).hasSessionAttributes()) {
            // 2.1设置缓存
            applyCacheSeconds(response, this.cacheSecondsForSessionAttributeHandlers);
        } else {
            // 2.2准备响应
            prepareResponse(response);
        }
    
        // 默认为 false,为 true 表示在同步块中执行 invokeHandlerMethod
        if (this.synchronizeOnSession) {
            HttpSession session = request.getSession(false);
            if (session != null) {
                Object mutex = WebUtils.getSessionMutex(session);
                synchronized (mutex) {
                    return invokeHandlerMethod(request, response, handlerMethod);
                }
            }
        }
        //3.处理器调用
        return invokeHandlerMethod(request, response, handlerMethod);
    }
    

    private ModelAndView invokeHandlerMethod(HttpServletRequest request, HttpServletResponse response,HandlerMethod handlerMethod) throws Exception {
    	ServletWebRequest webRequest = new ServletWebRequest(request, response);
    	WebDataBinderFactory binderFactory = getDataBinderFactory(handlerMethod);
    	ModelFactory modelFactory = getModelFactory(handlerMethod, binderFactory);
    	ServletInvocableHandlerMethod requestMappingMethod = createRequestMappingMethod(handlerMethod, binderFactory);
    
    	ModelAndViewContainer mavContainer = new ModelAndViewContainer();
    	mavContainer.addAllAttributes(RequestContextUtils.getInputFlashMap(request));
    	modelFactory.initModel(webRequest, mavContainer, requestMappingMethod);
    	mavContainer.setIgnoreDefaultModelOnRedirect(this.ignoreDefaultModelOnRedirect);
    
            //执行Controller中的RequestMapping注释的方法  
    	requestMappingMethod.invokeAndHandle(webRequest, mavContainer);
    	modelFactory.updateModel(webRequest, mavContainer);
    
    	if (mavContainer.isRequestHandled()) {
    		return null;
    	}
    	else {
    		ModelMap model = mavContainer.getModel();
                    //返回ModelAndView视图  
    		ModelAndView mav = new ModelAndView(mavContainer.getViewName(), model);
    		if (!mavContainer.isViewReference()) {
    			mav.setView((View) mavContainer.getView());
    		}
    		if (model instanceof RedirectAttributes) {
    			Map flashAttributes = ((RedirectAttributes) model).getFlashAttributes();
    			RequestContextUtils.getOutputFlashMap(request).putAll(flashAttributes);
    		}
    		return mav;
    	}
    }
    

    RequestMappingHandlerAdapter 实现类
    是采用反射机制调用url请求对应的Controller中的方法
    返回执行结果值

  • 相关阅读:
    数据集划分——train_test_split函数使用说明
    如何系统性的学习报关知识
    SpringBoot整合ActiveMQ步骤
    SWIG教程-对C++语言的封装《三》
    【考研】数据结构考点——直接选择排序
    3种好用的可视化图表工具分享,快进来看!
    nginx端口转发?
    MySQL主从复制与读写分离
    【云原生 • Docker】mysql、tomcat、nginx、redis 环境部署
    Spring MVC拦截器
  • 原文地址:https://blog.csdn.net/qq_25073223/article/details/127799690