学习资料:开涛的《跟我学SpringMVC.pdf》

  众所周知,springMVC是比较常用的web框架,通常整合spring使用。这里抛开spring,单纯的对springMVC做一下总结。

  概念

  HandlerMapping:处理器映射,对请求的URL进行映射为具体的处理器(如果有拦截器也包含拦截器,会将Handler和多个HandlerInterceptor封装为HandlerExecutionChain对象)

  HandlerAdapter:处理器适配器,适配不同类型的处理器,如Controller、AbstractController等

  Handler:处理器,即开发的Controller

  ModeAndView:模型和视图,未来会渲染为具体的视图

  ViewResolver:视图解析器,将逻辑视图名称转化为物理视图

  View:视图

  流程图

  源码简析(摘自DispatcherServlet源码部分)

  由于springMVC的配置本身就上一个servlet,所以跟踪代码doService()=>doDispatcher(),核心代码如下所示,中文注释自己加的

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 {
//对文件上传类型的数据进行处理,使用MultipartResolver解析
processedRequest = checkMultipart(request);
multipartRequestParsed = (processedRequest != request); //使用HandlerMapping进行映射,将一个Handler和多个HandlerInterceptor封装为HandlerExecutionChain对象
mappedHandler = getHandler(processedRequest);
if (mappedHandler == null || mappedHandler.getHandler() == null) {
noHandlerFound(processedRequest, response);
return;
} //根据Handler获取合适的Handler适配器
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 (logger.isDebugEnabled()) {
logger.debug("Last-Modified value for [" + getRequestUri(request) + "] is: " + lastModified);
}
if (new ServletWebRequest(request, response).checkNotModified(lastModified) && isGet) {
return;
}
} /*
* 调用拦截器的preHandle(),返回true继续执行下一个拦截器或处理器,返回false中断流程并执行执行成功的拦截器的afterCompletion()。
          */
if (!mappedHandler.applyPreHandle(processedRequest, response)) {
return;
} //使用HandlerAdapter执行Handler并返回ModeAndView
mv = ha.handle(processedRequest, response, mappedHandler.getHandler()); if (asyncManager.isConcurrentHandlingStarted()) {
return;
} applyDefaultViewName(processedRequest, mv);
//调用拦截器的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);
}
//处理转发结果,包含ModeAndView的视图解析,视图解析后调用拦截器的afterCompletion()函数
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);
}
}
}
}

最新文章

  1. “iTunes无法连接iPad,因为设备超时”解决办法
  2. LintCode Find Minimum In Rotated Sorted Array
  3. 【hihoCoder 1036】Trie图
  4. Django1.9开发博客(14)- 集成Xadmin
  5. h264
  6. Java Volatile相关文章目录
  7. android popupwindow低版本报空指针
  8. 3月25日html(六) Javascrip
  9. js面向对象编程:if中能够使用那些作为推断条件呢?
  10. 成都Uber优步司机快速注册攻略(外地车牌也可加入,不用现场培训)
  11. jQuery 复制节点的元素实现加入到购物车功能
  12. 【日常学习】【欧拉功能】codevs2296 荣誉的解决方案卫队的一个问题
  13. Linux chown
  14. 在MAC电脑上抓取iphone数据包的方法
  15. fiddler安装 与 https
  16. Linux-安装Windows字体
  17. expdp和impdp导入和导出数据
  18. Codeforces Round #284 (Div. 1) A. Crazy Town 计算几何
  19. ELASTIC 5.2部署并收集nginx日志
  20. Ubuntu16.04 安装 Django

热门文章

  1. MB Star C3 vs MB Star C4
  2. (转)MySQL- 5.7 sys schema笔记,mysql-schema
  3. vuex到底是什么?
  4. HTML5跨域请求--POST方式
  5. java设计模式之桥梁模式(Bridge)
  6. Android OpenGL教程-第四课【转】
  7. BNU44583——Star Trek: First Contact——————【01背包】
  8. shell里面的#!
  9. webservice log4net日志写入失败
  10. ps命令详解加例子