三、SpringMVC之用
看透spring MVC 源代码分析与实践 学习记录
01:客户端请求到前端控制器(即 DispatcherServlet ):
→ HttpServlet#doService(ServletRequest request, ServletResponse response)
→ FrameworkServlet的doGet,doPost等方法
→ FrameworkServlet#processRequest
→ DispatcherServlet#doService(HttpServletRequest request, HttpServletResponse response)
→ DispatcherServlet#doDispatch(HttpServletRequest request, HttpServletResponse response)
02:查找 Handler
→ mappedHandler = getHandler(processedRequest);
List<HandlerMapping> 遍历HandlerMapping查找Handler
返回值是 HandlerExecutionChain,包含了2部分内容: ① Handler对象、② 拦截器数组。
03:通过 Handler 查找 HandlerAdapter
→ HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
04:前置
→ if (!mappedHandler.applyPreHandle(processedRequest, response)) { return; }
05:HandlerAdapter执行Handler处理业务,返回ModelAndView
→ mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
06:后置
→ mappedHandler.applyPostHandle(processedRequest, response, mv);
07:渲染视图,响应客户端
→ processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);
→ render(mv, request, response);
视图解析器(ViewResolver)返回真正的视图对象(View)
→ view=resolveViewName(mv.getViewName(),mv.getModelInternal(),locale,request);
→ view.render(mv.getModelInternal(), request, response);
→ InternalResourceView#renderMergedOutputModel(mergedModel, getRequestToExpose(request), response);
→ RequestDispatcher rd = getRequestDispatcher(request, dispatcherPath);
→ if (useInclude(request, response)) {…
rd.include(request, response);
} else {…
rd.forward(request, response);
}
springMVC以一个servlet为入口,即 DispatcherServlet 。
HandlerMapping
HandlerAdapter