RequestMappingHandlerAdapter类简介说明
下文笔者讲述RequestMappingHandlerAdapter类的简介说明,如下所示:
它是一个HandlerAdapter接口的实现类
它真真意义上实现了HandlerAdapter接口的类,下面将依次讲述其方法的功能
是采用反射机制调用url请求对应的Controller中的方法
返回执行结果值
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中的方法
返回执行结果值
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


