起点:tomcat调用FrameworkServlet的service(ServletRequest req, ServletResponse res)方法:
1)FrameworkServlet的service(ServletRequest req, ServletResponse res) 方法转到2)
2)HttpServlet的service(ServletRequest req, ServletResponse res) 方法转到3)
3)FrameworkServlet的doGet(HttpServletRequest request, HttpServletResponse response) 方法转到4)
4)FrameworkServlet的processRequest(HttpServletRequest request, HttpServletResponse response) 方法转到5)
5)DispatcherServlet的doService(HttpServletRequest request, HttpServletResponse response) 方法转到6)
6)DispatcherServlet的doDispatch(HttpServletRequest request, HttpServletResponse response)//重点来依次调用7)8)9)10)
6.1)mappedHandler = getHandler(processedRequest);//利用7)
6.2)HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());//利用8)
6.3)mv = ha.handle(processedRequest, response, mappedHandler.getHandler());//利用9)和6.1)、6.2)的返回值
6.4)processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);//
7)DispatcherServlet的 HandlerExecutionChain getHandler(HttpServletRequest request)//找HandlerExecutionChain
7.1)属性List<HandlerMapping> handlerMappings;
7.2)HandlerExecutionChain handler = hm.getHandler(request);//内部执行7.2)
7.3)HandlerMethod getHandlerInternal(HttpServletRequest request);//内部执行7.3)
7.4)HandlerMethod lookupHandlerMethod(String lookupPath, HttpServletRequest request);//通过请求的路径,找到HandlerMethod
8)DispatcherServlet的 HandlerAdapter getHandlerAdapter(Object handler)//传入的是HandlerExecutionChain的handler
8.1)属性List<HandlerAdapter> handlerAdapters;//通过判断8.2)返回一个
8.2)ha.supports(handler);//如果成功,返回ha
9)ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler);//返回ModelAndView
9.1)ModelAndView handleInternal(HttpServletRequest request,HttpServletResponse response, HandlerMethod handlerMethod)
9.2)ModelAndView invokeHandlerMethod(HttpServletRequest request,HttpServletResponse response, HandlerMethod handlerMethod)
9.3)ModelAndView getModelAndView(ModelAndViewContainer mavContainer,ModelFactory modelFactory, NativeWebRequest webRequest)
10)processDispatchResult(HttpServletRequest request, HttpServletResponse response,HandlerExecutionChain mappedHandler, ModelAndView mv, Exception exception)//获得ModelAndView的view,并渲染
10.1)render(mv, request, response);//
10.2)render(ModelAndView mv, HttpServletRequest request, HttpServletResponse response)//
10.3)view = mv.getView();//获取view
10.4)view.render(mv.getModelInternal(), request, response);//渲染
总结:前面几部不重要,重要的是第6)步
1)获取HandlerExecutionChain{handler,interceptorList,interceptors};//通过遍历handlerMappings,获取HandlerExecutionChain
2)使用HandlerExecutionChain的handler,获取HandlerAdapter;//遍历handlerAdapters
3)使用HandlerAdapter通过HandlerExecutionChain得到ModelAndView
4)执行processDispatchResult,获取ModelAndView的View并渲染
名词解释:
handlermapping:处理器映射
HandlerAdapter:适配器
//非启动时执行,第一次时查询
protected void initStrategies(ApplicationContext context) {
initMultipartResolver(context);
initLocaleResolver(context);
initThemeResolver(context);
initHandlerMappings(context);
initHandlerAdapters(context);
initHandlerExceptionResolvers(context);
initRequestToViewNameTranslator(context);
initViewResolvers(context);
initFlashMapManager(context);
}
来源:CSDN
作者:孤竹彧
链接:https://blog.csdn.net/qq_33321609/article/details/104885553