1.springMVC执行流程示意图
2.代码分析springMVC执行流程图
1)例子项目结构
springmvc.xml
web.xml
(1)启动项目后我们访问 http://localhost:9898/hello 能够成功跳转到success页面,
我们访问http://localhost:9898/hello2.html
由于处理/hello2的handller并不存在,并且也没有配置<mvc:default-servlet-handler/>,所以出现如下:
控制台:
页面:
(2)更改springmvc.xml 添加<mvc:default-servlet-handler/>
springmvc.xml
我们再重启项目访问 http://localhost:9898/hello 结果如下
原本正常访问的也报错 解决:springmvc.xml 要开启注解驱动 <mvc:annotation-driven />
springmvc.xml
再次访问http://localhost:9898/hello 成功
访问:http://localhost:9898/hello2.jsp 成功跳转
3.源码跟踪
访问http://localhost:9898/hello 进入断点调试如图
主要执行过程再 DispathcerServlet的doDispatch方法其主要内容如下
/* @param request current HTTP request //当前的HttP请求
* @param response current HTTP response //当前的Http响应
* @throws Exception in case of any kind of processing failure //在任何类型执行失败的时候抛出异常
*/
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 {
//如果请求类型是multipart将通过MultipartResolver进行文件上传解析
processedRequest = checkMultipart(request);
multipartRequestParsed = processedRequest != request;
//确定处理当前请求的处理器,这个mappedHandler是HandlerExecutionChain类型,它包括了处理当前请求的处理器对象和多个拦截器默认的拦截器对象;
//此处的处理器为com.cqxy.springmvc.HelloSpringMVC.hello()和org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor
//mappedHandler 通过handlerMapping获得,在没有配置<mvc:default-servlet-handler/>的情况下如过请求没有对应的映射那么此时mappedHandler 将为null 如果配置了将返回默认的DefaultServletHttpRequestHandler
mappedHandler = getHandler(processedRequest);
if (mappedHandler == null || mappedHandler.getHandler() == null) {
//当没有请求所映射的处理器将返回404
noHandlerFound(processedRequest, response);
return;
}
// 确定当前请求的处理程序适配器,此处理器支持多种类型的处理器(HandlerExecutionChain中的处理器)通俗点说这里对数据进行处理如表单数据类型的校验,数据类型的转化,格式化等等
HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
// 获取http的请求方法
String method = request.getMethod();
boolean isGet = "GET".equals(method);
if (isGet || "HEAD".equals(method)) {
long lastModified = ha.getLastModified(request, mappedHandler.getHandler());
if (logger.isDebugEnabled()) {
String requestUri = urlPathHelper.getRequestUri(request);
logger.debug("Last-Modified value for [" + requestUri + "] is: " + lastModified);
}
if (new ServletWebRequest(request, response).checkNotModified(lastModified) && isGet) {
return;
}
}
//调用拦截器的PreHandle方法
if (!mappedHandler.applyPreHandle(processedRequest, response)) {
return;
}
try {
// 调用目标方法即调用controller所需要执行的方法
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
}finally {
if (asyncManager.isConcurrentHandlingStarted()) {
return;
}
}
applyDefaultViewName(request, mv);
//调用拦截器的PostHandle方法
mappedHandler.applyPostHandle(processedRequest, response, mv);
}catch (Exception ex) {
dispatchException = ex;
}
//处理视图
processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);
}catch (Exception ex) {
triggerAfterCompletion(processedRequest, response, mappedHandler, ex);
}catch (Error err) {
triggerAfterCompletionWithError(processedRequest, response, mappedHandler, err);
}finally {
if (asyncManager.isConcurrentHandlingStarted()) {
//调用拦截器的AfterConcurrentHandling方法
mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response);
return;
}
if (multipartRequestParsed) {
//清理多部分请求使用的任何资源
cleanupMultipart(processedRequest);
}
}
}