當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
请求成功得到返回数据还是走到catch_面试:SpringMVC在接收到请求后的调用细节是什么?...
生活随笔
收集整理的這篇文章主要介紹了
请求成功得到返回数据还是走到catch_面试:SpringMVC在接收到请求后的调用细节是什么?...
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
分享自己在Java方面的所思所想,希望你看完之后能有更多更深入的了解
本人微信公眾號(hào)(jwfy的學(xué)習(xí)分享),歡迎關(guān)注~
接收到一個(gè)新的請求之后,spring就會(huì)去根據(jù)請求的URL信息選擇具體的代碼塊去執(zhí)行操作,如圖就是接收到一個(gè)新的請求調(diào)用圖,從Tomcat開始直到把請求分發(fā)到spring中,最后到了doDispatch方法。
本篇學(xué)習(xí)筆記主要就是講一個(gè)新的請求被分發(fā)到spring中spring如何處理,至于如何掃描包中的controller,得到URL配置信息可以看 你是否清楚Spring MVC中的URL映射管理器的工作原理 ,而本篇主要介紹了
- 獲取執(zhí)行鏈
- 404頁面設(shè)置
- 獲取適配器
- LastModified
- 內(nèi)容方法調(diào)用(invoke)
- 視圖渲染
讓我們更加清楚的知道spring中一般的方法是如何確定調(diào)用的具體方法的,適配不同的模板引擎,達(dá)到渲染的地步,再者有時(shí)候又是API一般只需要返回json結(jié)構(gòu)的數(shù)據(jù),其背后的原理是如何實(shí)現(xiàn)的,以及我們在使用過程中如何避免出現(xiàn)的各種問題。
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; // 初始化設(shè)置模板為null Exception dispatchException = null; // 初始化異常為null try { processedRequest = checkMultipart(request); multipartRequestParsed = (processedRequest != request); // 文件上傳的相關(guān)設(shè)置和操作 mappedHandler = getHandler(processedRequest); // 根據(jù)request獲取對(duì)應(yīng)的請求執(zhí)行鏈 if (mappedHandler == null || mappedHandler.getHandler() == null) { // 如果沒有對(duì)應(yīng)的handler對(duì)于,則應(yīng)該是定義為404,通過noHandlerFound確認(rèn) noHandlerFound(processedRequest, response); return; } // 通過handler獲得合適的handler適配器 HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler()); String method = request.getMethod(); boolean isGet = "GET".equals(method); if (isGet || "HEAD".equals(method)) { // 是get方法或者h(yuǎn)ead方法 long lastModified = ha.getLastModified(request, mappedHandler.getHandler()); // 如果handler 是LastModified類,則獲取其lastModified值,否則返回-1 // lastModify 是spring添加了緩存機(jī)制,當(dāng)重復(fù)請求同樣的內(nèi)容,返回403,而不會(huì)返回真正的內(nèi)容,具體可看下面的LastModified機(jī)制這一小節(jié) if (logger.isDebugEnabled()) { logger.debug("Last-Modified value for [" + getRequestUri(request) + "] is: " + lastModified); } if (new ServletWebRequest(request, response).checkNotModified(lastModified) && isGet) { return; } } // 前置的handler預(yù)處理,就是獲取執(zhí)行鏈的攔截器,對(duì)請求進(jìn)行攔截處理 if (!mappedHandler.applyPreHandle(processedRequest, response)) { // 如果攔截器攔截成功,返回false,直接結(jié)束了 // 當(dāng)然在這其中攔截器肯定需要特定返回自身的內(nèi)容到response中,便于展示在頁面上 // 不過從頁面角度出發(fā)并沒有非常實(shí)質(zhì)性的攔截器處理,這點(diǎn)存疑? return; } // 真正的調(diào)用各自的執(zhí)行方法,返回ModelAndView后續(xù)在invoke這一小節(jié)細(xì)說 mv = ha.handle(processedRequest, response, mappedHandler.getHandler()); if (asyncManager.isConcurrentHandlingStarted()) { return; } applyDefaultViewName(processedRequest, mv); // 如果mv沒有包含有效的視圖,則從dispatch的viewNameTranslator屬性上獲取對(duì)應(yīng)的默認(rèn)視圖 mappedHandler.applyPostHandle(processedRequest, response, mv); // 攔截器的后置處理 } catch (Exception ex) { dispatchException = ex; } catch (Throwable err) { dispatchException = new NestedServletException("Handler dispatch failed總結(jié)
以上是生活随笔為你收集整理的请求成功得到返回数据还是走到catch_面试:SpringMVC在接收到请求后的调用细节是什么?...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux编程基础_第1篇 Linux系
- 下一篇: mono for android mys