1.从DispatcherServlet开始
public class DispatcherServlet extends FrameworkServlet
public abstract class FrameworkServlet extends HttpServletBean
implements ApplicationContextAware
public abstract class HttpServletBean extends HttpServlet
implements EnvironmentCapable, EnvironmentAware
Servlet都是从HttpServet的doPost()或者doGet()开始分发;
doService-->doDispatch()
以下是doDispatch方法的源码
(1)用户发送请求 由Dispatcher接受 调用doDispatch方法进行处理
protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpServletRequest processedRequest = request;
HandlerExecutionChain mappedHandler = null;
boolean multipartRequestParsed = false;
WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
try {
ModelAndView mv = null;
Exception dispatchException = null;
try {
//检查是否是文件上传请求
processedRequest = checkMultipart(request);
multipartRequestParsed = (processedRequest != request);
// Determine handler for the current request.
**//(2)根据当前处理器的映射信息找到处理当前请求的处理器执行链
//执行链中包含Handler映射信息和拦截器HandlerInteraptor 自定义拦截器需要实现HandlerInteraptor接 口或者实现HandlerInterceptorAdapter抽象类 实现 boolean preHandle方法(controller执行前) void postHandle(渲染也面前)和 void afterCompletion方法(容器关闭前)**
mappedHandler = getHandler(processedRequest);
if (mappedHandler == null) {
noHandlerFound(processedRequest, response);
return;
}
// Determine handler adapter for the current request.
**//(3) DispatcherServlet接收到HandlerMapping返回的HandlerExcutorChain后,调用HandlerAdapter请求执行具体的Handler(Controller)。**
HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
// Process last-modified header, if supported by the handler.
String method = request.getMethod();
boolean isGet = "GET".equals(method);
if (isGet || "HEAD".equals(method)) {
long lastModified = ha.getLastModified(request, mappedHandler.getHandler());
if (new ServletWebRequest(request, response).checkNotModified(lastModified) && isGet) {
return;
}
}
//(4)执行拦截器的preHandle方法
if (!mappedHandler.applyPreHandle(processedRequest, response)) {
return;
}
// Actually invoke the handler.
//(5)执行Controller中请求对应的RequestMapping方法 就是执行Controller 返回ModelAndView
// 1.ModelAttrbuite标注的方法提前运行 返回值存到隐含模型RequestScope中
//2.确定方法中的请求参数 如果标了@RequestParam注解 就从requestParam中找 找不到就报错
// 如果没有标注注解 就从隐含模型中拿 如果没有且标了SessionAttrrubite注解就从中拿(标了注解还没有就报错)如果都没有 就利用反射创建一个新的对象
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
if (asyncManager.isConcurrentHandlingStarted()) {
return;
}
applyDefaultViewName(processedRequest, mv);
//(6)执行拦截器的postHandle
mappedHandler.applyPostHandle(processedRequest, response, mv);
}
catch (Exception ex) {
dispatchException = ex;
}
catch (Throwable err) {
// As of 4.3, we're processing Errors thrown from handler methods as well,
// making them available for @ExceptionHandler methods and other scenarios.
dispatchException = new NestedServletException("Handler dispatch failed", err);
}
//(7)渲染页面
//1。如果有异常 就是用异常解析器处理异常 返回ModelAndView (自定义异常页面@ControllerAdvice加@ExceptionHandler)
//2.调用render方法渲染页面 InternalResourceViewResolver根据prefix和suffix得到完整的视图名 渲染页面
processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);
}
catch (Exception ex) {
triggerAfterCompletion(processedRequest, response, mappedHandler, ex);
}
catch (Throwable err) {
triggerAfterCompletion(processedRequest, response, mappedHandler,
new NestedServletException("Handler processing failed", err));
}
finally {
if (asyncManager.isConcurrentHandlingStarted()) {
// Instead of postHandle and afterCompletion
if (mappedHandler != null) {
mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response);
}
}
else {
// Clean up any resources used by a multipart request.
if (multipartRequestParsed) {
cleanupMultipart(processedRequest);
}
}
}
}