欧美成人午夜免费全部完,亚洲午夜福利精品久久,а√最新版在线天堂,另类亚洲综合区图片小说区,亚洲欧美日韩精品色xxx

千鋒扣丁學(xué)堂Java培訓(xùn)之帶你從代碼看spring mvc請求處理過程

2019-06-25 13:50:57 1160瀏覽

今天千鋒扣丁學(xué)堂Java培訓(xùn)老師給大家分享一篇關(guān)于從源碼角度看spring mvc的請求處理過程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,下面我們一起來看一下吧。



請求處理的過程:

1.DispatcherServelt作為前端控制器,攔截request對象。

2.DispatcherServlet接收到request對象之后,查詢HandlerMapping,得到一個HandlerExecutionChain對象。

3.DispatcherServlet將Handler對象交由HandlerAdapter(適配器模式的典型應(yīng)用),調(diào)用相應(yīng)的controller方法。

4.Controller方法返回ModelAndView對象,DispatcherServlet將view交由ViewResolver進(jìn)行解析,得到相應(yīng)的視圖。用model渲染視圖。

5.返回響應(yīng)結(jié)果。

整個過程的流程其實就是DispatcherServelt中doDispatch()方法的調(diào)用過程。

protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
//第一步攔截request對象,doDispatch()方法在doService()方法中被調(diào)用,request對象是經(jīng)過處理的。
HttpServletRequest processedRequest = request;
HandlerExecutionChain mappedHandler = null;
boolean multipartRequestParsed = false;
WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
try {
ModelAndView mv = null;
Exception dispatchException = null;
try {
//和文件的上傳和下載有關(guān)系,判斷請求的類型是否是multipart類型
processedRequest = checkMultipart(request);
multipartRequestParsed = (processedRequest != request);
// Determine handler for the current request.
//主要看這里,這里是得到HandlerExecutionChain的方法。關(guān)于Handler()方法向下看
mappedHandler = getHandler(processedRequest);
if (mappedHandler == null || mappedHandler.getHandler() == null) {
noHandlerFound(processedRequest, response);
return;
}
// Determine handler adapter for the current request.
//這里已經(jīng)獲取到HandlerExecutionChain對象,接下來就要獲取HandlerAdapter對象,調(diào)用Handler對象的方法。
HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
// Process last-modified header, if supported by the handler
//有關(guān)瀏覽器緩存的設(shè)定(304)
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;
}
}
//pan'du
if (!mappedHandler.applyPreHandle(processedRequest, response)) {
return;
}
// Actually invoke the handler.
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
if (asyncManager.isConcurrentHandlingStarted()) {
return;
}
//解析視圖,數(shù)據(jù)渲染
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 and afterCompletion
if (mappedHandler != null) {
mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response);
}
}
else {
// Clean up any resources used by a multipart request.
if (multipartRequestParsed) {
cleanupMultipart(processedRequest);
}
}
}
}
protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
//遍歷HandlerMappingList對象(存儲若干個HandlerMapping對象),不斷調(diào)用,直到不為空為止,否則返回null
for (HandlerMapping hm : this.handlerMappings) {
if (logger.isTraceEnabled()) {
logger.trace(
"Testing handler map [" + hm + "] in DispatcherServlet with name '" + getServletName() + "'");
}
HandlerExecutionChain handler = hm.getHandler(request);
if (handler != null) {
return handler;
}
}
return null;
}

以上就是關(guān)于千鋒扣丁學(xué)堂Java培訓(xùn)之帶你從代碼看spring mvc請求處理過程的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,想要了解更多關(guān)于Java開發(fā)方面內(nèi)容的小伙伴,請關(guān)注扣丁學(xué)堂Java培訓(xùn)官網(wǎng)、微信等平臺,扣丁學(xué)堂IT職業(yè)在線學(xué)習(xí)教育有專業(yè)的Java講師為您指導(dǎo),此外扣丁學(xué)堂老師精心推出的Java視頻教程定能讓你快速掌握J(rèn)ava從入門到精通開發(fā)實戰(zhàn)技能??鄱W(xué)堂Java技術(shù)交流群:850353792。


                        JavaEE/微服務(wù)/源碼解析/分布式/企業(yè)級架構(gòu)【VIP體驗課】


     【關(guān)注微信公眾號獲取更多學(xué)習(xí)資料】       【掃碼進(jìn)入JavaEE/微服務(wù)VIP免費(fèi)公開課】  



查看更多關(guān)于“Java開發(fā)資訊”的相關(guān)文章>>

標(biāo)簽: Java培訓(xùn) Java視頻教程 Java多線程 Java面試題 Java學(xué)習(xí)視頻 Java開發(fā)

熱門專區(qū)

暫無熱門資訊

課程推薦

微信
微博
15311698296

全國免費(fèi)咨詢熱線

郵箱:codingke@1000phone.com

官方群:148715490

北京千鋒互聯(lián)科技有限公司版權(quán)所有   北京市海淀區(qū)寶盛北里西區(qū)28號中關(guān)村智誠科創(chuàng)大廈4層
京ICP備2021002079號-2   Copyright ? 2017 - 2022
返回頂部 返回頂部