SpringMVC学习——组件解析

20 篇文章 0 订阅

SpringMVC学习——组件解析

Spring MVC框架结构图


前端控制器(DispatcherServlet)

 

控制器组件(Controller)

 

处理器映射器组件(HandlerMapping)

 

处理器适配器组件(HandlerAdapter)



拦截器组件(HandlerInterceptor)


视图解析器组件(ViewResolver)

 

视图组件(View)

前端控制器流程处理关键方法(doDispatch)源码解析:

protected voiddoDispatch(HttpServletRequest request, HttpServletResponse response) throwsException {

HttpServletRequest processedRequest =request;

HandlerExecutionChain mappedHandler = null;

boolean multipartRequestParsed = false;

 

WebAsyncManager asyncManager =WebAsyncUtils.getAsyncManager(request);

 

try {

ModelAndView mv = null;

Exception dispatchException = null;

 

try {

// 检查并且包装multipart/form-data请求

processedRequest = checkMultipart(request);

multipartRequestParsed = processedRequest!= request;

 

// 根据请求去匹配HandlerMapping,并且通过HandlerMapping返回HandlerExecutionChain

mappedHandler =getHandler(processedRequest, false);

if (mappedHandler == null ||mappedHandler.getHandler() == null) {

noHandlerFound(processedRequest, response);

return;

}

 

// 根据Handler匹配HandlerAdapter

HandlerAdapter ha =getHandlerAdapter(mappedHandler.getHandler());

 

// 验证HTTP头部last-modified

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;

}

}

 

// 执行前置通知

if(!mappedHandler.applyPreHandle(processedRequest, response)) {

return;

}

 

try {

// 解析方法参数,注入方法参数,并且执行Handler;最后返回ModelAndView;

mv = ha.handle(processedRequest, response,mappedHandler.getHandler());

}

finally {

if(asyncManager.isConcurrentHandlingStarted()) {

return;

}

}

 

// 设置默认视图

applyDefaultViewName(request, mv);

// 执行后置通知

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()){

// Instead of postHandle andafterCompletion

mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest,response);

return;

}

// 清除multipart/form-data资源

if (multipartRequestParsed) {

cleanupMultipart(processedRequest);

}

}

}

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值