Spring MVC中的HandlerMapping与HandlerAdapter(2)

getHandlerAdapter,获取HandlerAdapter,它包含一个handle方法,负责调用真实的页面处理器进行请求处理并返回一个ModelAndView。HandlerAdpter里面有一些常见的处理,比如消息转移,参数处理等,详见此图:里面的argumentResolvers可以用来处理请求的参数,messageConverts是作消息转换等等。

Spring MVC中的HandlerMapping与HandlerAdapter

HandlerAdapter.handle,执行真实页面处理器的处理请求。

请求时序图(只关注HandlerMapping与HandlerAdapter)

doDispath获取页面处理器,然后根据页面处理器获取对应的HanlerAdapter,最后由HanlerAdaper来调用页面处理器的方法。

protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception { //初始化省略 try { ModelAndView mv = null; Exception dispatchException = null; try { processedRequest = checkMultipart(request); multipartRequestParsed = (processedRequest != request); // Determine handler for the current request. mappedHandler = getHandler(processedRequest); if (mappedHandler == null || mappedHandler.getHandler() == null) { noHandlerFound(processedRequest, response); return; } // Determine handler adapter for the current request. HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler()); //其它逻辑省略 // Actually invoke the handler. mv = ha.handle(processedRequest, response, mappedHandler.getHandler()); //其它逻辑省略 } catch (Exception ex) { dispatchException = ex; } processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException); } //异常逻辑省略 }

具体的调用逻辑比较复杂,只选取与HandlerMapping与HandlerAdaper的部分,时序图图如下:

Spring MVC中的HandlerMapping与HandlerAdapter

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/76ed2d4f6e0996f256985ce81f188d0c.html