1.Spring MVC通过前置控制器DispatcherServlet处理客户端请求,这里用到了DispatcherServlet的doService方法中的doDispatch
DispatcherServlet.java
protected void doService(HttpServletRequest request, HttpServletResponse response) throws Exception {
logRequest(request);`
.....省略其他代码
try {
doDispatch(request, response);
}
....省略其他代码
}
2.在doDispatch方法中,通过将request请求从handlerMappings中获取一个handler,这里返回的是一个叫HandlerExecutionChain
protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
if (this.handlerMappings != null) {
for (HandlerMapping mapping : this.handlerMappings) {
HandlerExecutionChain handler = mapping.getHandler(request);
if (handler != null) {
return handler;
}
}
}
return null;
}
3.用handler获取HandlerAdapter
protected HandlerAdapter getHandlerAdapter(Object handler) throws ServletException {
if (this.handlerAdapters != null) {
for (HandlerAdapter adapter : this.handlerAdapters) {
if (adapter.supports(handler)) {
return adapter;
}
}
}
throw new ServletException("No adapter for handler [" + handler +
"]: The DispatcherServlet configuration needs to include a HandlerAdapter that supports this handler");
}
4.执行HandlerExecutionChain中的拦截器操作
5.通过HandlerAdapter执行真正的handler操作,返回ModleAndView
6.获取默认的viewName
7.执行拦截器的postHandle方法
mappedHandler.applyPostHandle(processedRequest, response, mv);
8.通过viewResolvers返回view
参考文档: https://blog.csdn.net/u013219087/article/details/80649650



