{"id":3012,"date":"2022-12-31T17:12:43","date_gmt":"2022-12-31T09:12:43","guid":{"rendered":"https:\/\/qaqaq.top\/?p=3012"},"modified":"2022-12-31T19:37:50","modified_gmt":"2022-12-31T11:37:50","slug":"springmvc%e6%89%a7%e8%a1%8c%e6%b5%81%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/qaqaq.top\/?p=3012","title":{"rendered":"SpringMVC\u6267\u884c\u6d41\u7a0b"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">1\u3001SpringMVC\u5e38\u7528\u7ec4\u4ef6<\/h2>\n\n\n\n<p>DispatcherServlet\uff1a<strong>\u524d\u7aef\u63a7\u5236\u5668<\/strong>\uff0c\u4e0d\u9700\u8981\u5de5\u7a0b\u5e08\u5f00\u53d1\uff0c\u7531\u6846\u67b6\u63d0\u4f9b<\/p>\n\n\n\n<p>\u4f5c\u7528\uff1a\u7edf\u4e00\u5904\u7406\u8bf7\u6c42\u548c\u54cd\u5e94\uff0c\u6574\u4e2a\u6d41\u7a0b\u63a7\u5236\u7684\u4e2d\u5fc3\uff0c\u7531\u5b83\u8c03\u7528\u5176\u5b83\u7ec4\u4ef6\u5904\u7406\u7528\u6237\u7684\u8bf7\u6c42<\/p>\n\n\n\n<p>HandlerMapping\uff1a<strong>\u5904\u7406\u5668\u6620\u5c04\u5668<\/strong>\uff0c\u4e0d\u9700\u8981\u5de5\u7a0b\u5e08\u5f00\u53d1\uff0c\u7531\u6846\u67b6\u63d0\u4f9b<\/p>\n\n\n\n<p>\u4f5c\u7528\uff1a\u6839\u636e\u8bf7\u6c42\u7684url\u3001method\u7b49\u4fe1\u606f\u67e5\u627eHandler\uff0c\u5373\u63a7\u5236\u5668\u65b9\u6cd5<\/p>\n\n\n\n<p>Handler\uff1a<strong>\u5904\u7406\u5668<\/strong>\uff0c\u9700\u8981\u5de5\u7a0b\u5e08\u5f00\u53d1<\/p>\n\n\n\n<p>\u4f5c\u7528\uff1a\u5728DispatcherServlet\u7684\u63a7\u5236\u4e0bHandler\u5bf9\u5177\u4f53\u7684\u7528\u6237\u8bf7\u6c42\u8fdb\u884c\u5904\u7406<\/p>\n\n\n\n<p>HandlerAdapter\uff1a<strong>\u5904\u7406\u5668\u9002\u914d\u5668<\/strong>\uff0c\u4e0d\u9700\u8981\u5de5\u7a0b\u5e08\u5f00\u53d1\uff0c\u7531\u6846\u67b6\u63d0\u4f9b<\/p>\n\n\n\n<p>\u4f5c\u7528\uff1a\u901a\u8fc7HandlerAdapter\u5bf9\u5904\u7406\u5668\uff08\u63a7\u5236\u5668\u65b9\u6cd5\uff09\u8fdb\u884c\u6267\u884c<\/p>\n\n\n\n<p>ViewResolver\uff1a<strong>\u89c6\u56fe\u89e3\u6790\u5668<\/strong>\uff0c\u4e0d\u9700\u8981\u5de5\u7a0b\u5e08\u5f00\u53d1\uff0c\u7531\u6846\u67b6\u63d0\u4f9b<\/p>\n\n\n\n<p>\u4f5c\u7528\uff1a\u8fdb\u884c\u89c6\u56fe\u89e3\u6790\uff0c\u5f97\u5230\u76f8\u5e94\u7684\u89c6\u56fe\uff0c\u4f8b\u5982\uff1aThymeleafView\u3001InternalResourceView\u3001<br>RedirectView<\/p>\n\n\n\n<p>View\uff1a<strong>\u89c6\u56fe<\/strong><\/p>\n\n\n\n<p>\u4f5c\u7528\uff1a\u5c06\u6a21\u578b\u6570\u636e\u901a\u8fc7\u9875\u9762\u5c55\u793a\u7ed9\u7528\u6237<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2\u3001DispatcherServlet\u521d\u59cb\u5316\u8fc7\u7a0b<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\u4eceServlet.java\u63a5\u53e3\u7684init\u65b9\u6cd5\u5f00\u59cb\u627e<\/code><\/pre>\n\n\n\n<p>DispatcherServlet \u672c\u8d28\u4e0a\u662f\u4e00\u4e2a Servlet\uff0c\u6240\u4ee5\u5929\u7136\u7684\u9075\u5faa Servlet \u7684\u751f\u547d\u5468\u671f\u3002\u6240\u4ee5\u5b8f\u89c2\u4e0a\u662f Servlet<br>\u751f\u547d\u5468\u671f\u6765\u8fdb\u884c\u8c03\u5ea6\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/qaqaq.top\/wp-content\/uploads\/2022\/12\/image-405.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"476\" height=\"855\" data-original=\"https:\/\/qaqaq.top\/wp-content\/uploads\/2022\/12\/image-405.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-3013\"  sizes=\"(max-width: 476px) 100vw, 476px\" \/><\/div><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">a&gt;\u521d\u59cb\u5316WebApplicationContext<\/h3>\n\n\n\n<p>\u6240\u5728\u7c7b\uff1aorg.springframework.web.servlet.FrameworkServlet<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\tprotected WebApplicationContext initWebApplicationContext() {\n\t\tWebApplicationContext rootContext =\n\t\t\t\tWebApplicationContextUtils.getWebApplicationContext(getServletContext());\n\t\tWebApplicationContext wac = null;\n\n\t\tif (this.webApplicationContext != null) {\n\t\t\t\/\/ A context instance was injected at construction time -> use it\n\t\t\twac = this.webApplicationContext;\n\t\t\tif (wac instanceof ConfigurableWebApplicationContext) {\n\t\t\t\tConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) wac;\n\t\t\t\tif (!cwac.isActive()) {\n\t\t\t\t\t\/\/ The context has not yet been refreshed -> provide services such as\n\t\t\t\t\t\/\/ setting the parent context, setting the application context id, etc\n\t\t\t\t\tif (cwac.getParent() == null) {\n\t\t\t\t\t\t\/\/ The context instance was injected without an explicit parent -> set\n\t\t\t\t\t\t\/\/ the root application context (if any; may be null) as the parent\n\t\t\t\t\t\tcwac.setParent(rootContext);\n\t\t\t\t\t}\n\t\t\t\t\tconfigureAndRefreshWebApplicationContext(cwac);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (wac == null) {\n\t\t\t\/\/ No context instance was injected at construction time -> see if one\n\t\t\t\/\/ has been registered in the servlet context. If one exists, it is assumed\n\t\t\t\/\/ that the parent context (if any) has already been set and that the\n\t\t\t\/\/ user has performed any initialization such as setting the context id\n\t\t\twac = findWebApplicationContext();\n\t\t}\n\t\tif (wac == null) {\n\t\t\t\/\/ No context instance is defined for this servlet -> create a local one\n\t\t\t\/\/ <strong>\u521b\u5efaWebApplicationContext<\/strong>\n\t\t\twac = createWebApplicationContext(rootContext);\n\t\t}\n\n\t\tif (!this.refreshEventReceived) {\n\t\t\t\/\/ Either the context is not a ConfigurableApplicationContext with refresh\n\t\t\t\/\/ support or the context injected at construction time had already been\n\t\t\t\/\/ refreshed -> trigger initial onRefresh manually here.\n\t\t\tsynchronized (this.onRefreshMonitor) {\n\t\t\t\t\/\/ <strong>\u5237\u65b0WebApplicationContext<\/strong>\n\t\t\t\tonRefresh(wac);\n\t\t\t}\n\t\t}\n\n\t\tif (this.publishContext) {\n\t\t\t\/\/ Publish the context as a servlet context attribute.\n\t\t\t\/\/ <strong>\u5c06IOC\u5bb9\u5668\u5728\u5e94\u7528\u57df\u5171\u4eab<\/strong>\n\t\t\tString attrName = getServletContextAttributeName();\n\t\t\tgetServletContext().setAttribute(attrName, wac);\n\t\t}\n\n\t\treturn wac;\n\t}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">b>\u521b\u5efaWebApplicationContext<\/h3>\n\n\n\n<p>\u6240\u5728\u7c7b\uff1aorg.springframework.web.servlet.FrameworkServlet<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\tprotected WebApplicationContext createWebApplicationContext(@Nullable ApplicationContext parent) {\r\n\t\tClass&lt;?> contextClass = getContextClass();\r\n\t\tif (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) {\r\n\t\t\tthrow new ApplicationContextException(\r\n\t\t\t\t\t\"Fatal initialization error in servlet with name '\" + getServletName() +\r\n\t\t\t\t\t\"': custom WebApplicationContext class &#91;\" + contextClass.getName() +\r\n\t\t\t\t\t\"] is not of type ConfigurableWebApplicationContext\");\r\n\t\t}\n\r\t\t\/\/ <strong>\u901a\u8fc7\u53cd\u5c04\u521b\u5efa IOC \u5bb9\u5668\u5bf9\u8c61<\/strong>\n\t\tConfigurableWebApplicationContext wac =\r\n\t\t\t\t(ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);\r\n\r\n\t\twac.setEnvironment(getEnvironment());\n\r\t\t\/\/ <strong>\u8bbe\u7f6e\u7236\u5bb9\u5668<\/strong>\n\t\twac.setParent(parent);\r\n\t\tString configLocation = getContextConfigLocation();\r\n\t\tif (configLocation != null) {\r\n\t\t\twac.setConfigLocation(configLocation);\r\n\t\t}\r\n\t\tconfigureAndRefreshWebApplicationContext(wac);\r\n\r\n\t\treturn wac;\r\n\t}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">c>DispatcherServlet\u521d\u59cb\u5316\u7b56\u7565<\/h3>\n\n\n\n<p>FrameworkServlet\u521b\u5efaWebApplicationContext\u540e\uff0c\u5237\u65b0\u5bb9\u5668\uff0c\u8c03\u7528onRefresh(wac)\uff0c\u6b64\u65b9\u6cd5\u5728<br>DispatcherServlet\u4e2d\u8fdb\u884c\u4e86\u91cd\u5199\uff0c\u8c03\u7528\u4e86initStrategies(context)\u65b9\u6cd5\uff0c\u521d\u59cb\u5316\u7b56\u7565\uff0c\u5373\u521d\u59cb\u5316<br>DispatcherServlet\u7684\u5404\u4e2a\u7ec4\u4ef6<\/p>\n\n\n\n<p>\u6240\u5728\u7c7b\uff1aorg.springframework.web.servlet.DispatcherServlet<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\tprotected void initStrategies(ApplicationContext context) {\r\n\t\tinitMultipartResolver(context);\r\n\t\tinitLocaleResolver(context);\r\n\t\tinitThemeResolver(context);\r\n\t\tinitHandlerMappings(context);\r\n\t\tinitHandlerAdapters(context);\r\n\t\tinitHandlerExceptionResolvers(context);\r\n\t\tinitRequestToViewNameTranslator(context);\r\n\t\tinitViewResolvers(context);\r\n\t\tinitFlashMapManager(context);\r\n\t}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">3\u3001DispatcherServlet\u8c03\u7528\u7ec4\u4ef6\u5904\u7406\u8bf7\u6c42<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\u4eceServlet.java\u63a5\u53e3\u7684init\u65b9\u6cd5\u5f00\u59cb\u627e<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">a>processRequest()<\/h3>\n\n\n\n<p>FrameworkServlet\u91cd\u5199HttpServlet\u4e2d\u7684service()\u548cdoXxx()\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\u8c03\u7528\u4e86<br>processRequest(request, response)<\/p>\n\n\n\n<p>\u6240\u5728\u7c7b\uff1aorg.springframework.web.servlet.FrameworkServlet<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\tprotected final void processRequest(HttpServletRequest request, HttpServletResponse response)\r\n\t\t\tthrows ServletException, IOException {\r\n\r\n\t\tlong startTime = System.currentTimeMillis();\r\n\t\tThrowable failureCause = null;\r\n\r\n\t\tLocaleContext previousLocaleContext = LocaleContextHolder.getLocaleContext();\r\n\t\tLocaleContext localeContext = buildLocaleContext(request);\r\n\r\n\t\tRequestAttributes previousAttributes = RequestContextHolder.getRequestAttributes();\r\n\t\tServletRequestAttributes requestAttributes = buildRequestAttributes(request, response, previousAttributes);\r\n\r\n\t\tWebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);\r\n\t\tasyncManager.registerCallableInterceptor(FrameworkServlet.class.getName(), new RequestBindingInterceptor());\r\n\r\n\t\tinitContextHolders(request, localeContext, requestAttributes);\r\n\r\n\t\ttry {\n\r\t\t\t\/\/ <strong>\u6267\u884c\u670d\u52a1\uff0cdoService()\u662f\u4e00\u4e2a\u62bd\u8c61\u65b9\u6cd5\uff0c\u5728DispatcherServlet\u4e2d\u8fdb\u884c\u4e86\u91cd\u5199<\/strong>\n\t\t\tdoService(request, response);\r\n\t\t}\r\n\t\tcatch (ServletException | IOException ex) {\r\n\t\t\tfailureCause = ex;\r\n\t\t\tthrow ex;\r\n\t\t}\r\n\t\tcatch (Throwable ex) {\r\n\t\t\tfailureCause = ex;\r\n\t\t\tthrow new NestedServletException(\"Request processing failed\", ex);\r\n\t\t}\r\n\r\n\t\tfinally {\r\n\t\t\tresetContextHolders(request, previousLocaleContext, previousAttributes);\r\n\t\t\tif (requestAttributes != null) {\r\n\t\t\t\trequestAttributes.requestCompleted();\r\n\t\t\t}\r\n\t\t\tlogResult(request, response, failureCause, asyncManager);\r\n\t\t\tpublishRequestHandledEvent(request, response, startTime, failureCause);\r\n\t\t}\r\n\t}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">b>doService()<\/h3>\n\n\n\n<p>\u6240\u5728\u7c7b\uff1aorg.springframework.web.servlet.DispatcherServlet<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\t@Override\n\tprotected void doService(HttpServletRequest request, HttpServletResponse response) throws Exception {\n\t\tlogRequest(request);\n\n\t\t\/\/ Keep a snapshot of the request attributes in case of an include,\n\t\t\/\/ to be able to restore the original attributes after the include.\n\t\tMap&lt;String, Object> attributesSnapshot = null;\n\t\tif (WebUtils.isIncludeRequest(request)) {\n\t\t\tattributesSnapshot = new HashMap&lt;>();\n\t\t\tEnumeration&lt;?> attrNames = request.getAttributeNames();\n\t\t\twhile (attrNames.hasMoreElements()) {\n\t\t\t\tString attrName = (String) attrNames.nextElement();\n\t\t\t\tif (this.cleanupAfterInclude || attrName.startsWith(DEFAULT_STRATEGIES_PREFIX)) {\n\t\t\t\t\tattributesSnapshot.put(attrName, request.getAttribute(attrName));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t\/\/ Make framework objects available to handlers and view objects.\n\t\trequest.setAttribute(WEB_APPLICATION_CONTEXT_ATTRIBUTE, getWebApplicationContext());\n\t\trequest.setAttribute(LOCALE_RESOLVER_ATTRIBUTE, this.localeResolver);\n\t\trequest.setAttribute(THEME_RESOLVER_ATTRIBUTE, this.themeResolver);\n\t\trequest.setAttribute(THEME_SOURCE_ATTRIBUTE, getThemeSource());\n\n\t\tif (this.flashMapManager != null) {\n\t\t\tFlashMap inputFlashMap = this.flashMapManager.retrieveAndUpdate(request, response);\n\t\t\tif (inputFlashMap != null) {\n\t\t\t\trequest.setAttribute(INPUT_FLASH_MAP_ATTRIBUTE, Collections.unmodifiableMap(inputFlashMap));\n\t\t\t}\n\t\t\trequest.setAttribute(OUTPUT_FLASH_MAP_ATTRIBUTE, new FlashMap());\n\t\t\trequest.setAttribute(FLASH_MAP_MANAGER_ATTRIBUTE, this.flashMapManager);\n\t\t}\n\n\t\tRequestPath requestPath = null;\n\t\tif (this.parseRequestPath &amp;&amp; !ServletRequestPathUtils.hasParsedRequestPath(request)) {\n\t\t\trequestPath = ServletRequestPathUtils.parseAndCache(request);\n\t\t}\n\n\t\ttry {\n\n\t\t\t\/\/ <strong>\u5904\u7406\u8bf7\u6c42\u548c\u54cd\u5e94<\/strong>\n\t\t\tdoDispatch(request, response);\n\t\t}\n\t\tfinally {\n\t\t\tif (!WebAsyncUtils.getAsyncManager(request).isConcurrentHandlingStarted()) {\n\t\t\t\t\/\/ Restore the original attribute snapshot, in case of an include.\n\t\t\t\tif (attributesSnapshot != null) {\n\t\t\t\t\trestoreAttributesAfterInclude(request, attributesSnapshot);\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (requestPath != null) {\n\t\t\t\tServletRequestPathUtils.clearParsedRequestPath(request);\n\t\t\t}\n\t\t}\n\t}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">c>doDispatch()<\/h3>\n\n\n\n<p>\u6240\u5728\u7c7b\uff1aorg.springframework.web.servlet.DispatcherServlet<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\tprotected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {\n\t\tHttpServletRequest processedRequest = request;\n\t\tHandlerExecutionChain mappedHandler = null;\n\t\tboolean multipartRequestParsed = false;\n\n\t\tWebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);\n\n\t\ttry {\n\t\t\tModelAndView mv = null;\n\t\t\tException dispatchException = null;\n\n\t\t\ttry {\n\t\t\t\tprocessedRequest = checkMultipart(request);\n\t\t\t\tmultipartRequestParsed = (processedRequest != request);\n\n\t\t\t\t\/\/ Determine handler for the current request.\n\n\t\t\t\t\/*\n\t\t\t\t\t<strong>mappedHandler\uff1a\u8c03\u7528\u94fe\n\t\t\t\t\t\u5305\u542bhandler\u3001interceptorList\u3001interceptorIndex\n\t\t\t\t\thandler\uff1a\u6d4f\u89c8\u5668\u53d1\u9001\u7684\u8bf7\u6c42\u6240\u5339\u914d\u7684\u63a7\u5236\u5668\u65b9\u6cd5\n\t\t\t\t\tinterceptorList\uff1a\u5904\u7406\u63a7\u5236\u5668\u65b9\u6cd5\u7684\u6240\u6709\u62e6\u622a\u5668\u96c6\u5408\n\t\t\t\t\tinterceptorIndex\uff1a\u62e6\u622a\u5668\u7d22\u5f15\uff0c\u63a7\u5236\u62e6\u622a\u5668afterCompletion()\u7684\u6267\u884c<\/strong>\n\t\t\t\t*\/\n\t\t\t\tmappedHandler = getHandler(processedRequest);\n\t\t\t\tif (mappedHandler == null) {\n\t\t\t\t\tnoHandlerFound(processedRequest, response);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\t\/\/ Determine handler adapter for the current request.\n\t\t\t\t\/\/ <strong>\u901a\u8fc7\u63a7\u5236\u5668\u65b9\u6cd5\u521b\u5efa\u76f8\u5e94\u7684\u5904\u7406\u5668\u9002\u914d\u5668\uff0c\u8c03\u7528\u6240\u5bf9\u5e94\u7684\u63a7\u5236\u5668\u65b9\u6cd5<\/strong>\n\t\t\t\tHandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());\n\n\t\t\t\t\/\/ Process last-modified header, if supported by the handler.\n\t\t\t\tString method = request.getMethod();\n\t\t\t\tboolean isGet = \"GET\".equals(method);\n\t\t\t\tif (isGet || \"HEAD\".equals(method)) {\n\t\t\t\t\tlong lastModified = ha.getLastModified(request, mappedHandler.getHandler());\n\t\t\t\t\tif (new ServletWebRequest(request, response).checkNotModified(lastModified) &amp;&amp; isGet) {\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\t\/\/ <strong>\u8c03\u7528\u62e6\u622a\u5668\u7684preHandle()<\/strong>\n\t\t\t\tif (!mappedHandler.applyPreHandle(processedRequest, response)) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\t\/\/ Actually invoke the handler.\n\t\t\t\t\/\/ <strong>\u7531\u5904\u7406\u5668\u9002\u914d\u5668\u8c03\u7528\u5177\u4f53\u7684\u63a7\u5236\u5668\u65b9\u6cd5\uff0c\u6700\u7ec8\u83b7\u5f97ModelAndView\u5bf9\u8c61<\/strong>\n\t\t\t\tmv = ha.handle(processedRequest, response, mappedHandler.getHandler());\n\n\t\t\t\tif (asyncManager.isConcurrentHandlingStarted()) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tapplyDefaultViewName(processedRequest, mv);\n\t\t\t\t\/\/ <strong>\u8c03\u7528\u62e6\u622a\u5668\u7684postHandle()<\/strong>\n\t\t\t\tmappedHandler.applyPostHandle(processedRequest, response, mv);\n\t\t\t}\n\t\t\tcatch (Exception ex) {\n\t\t\t\tdispatchException = ex;\n\t\t\t}\n\t\t\tcatch (Throwable err) {\n\t\t\t\t\/\/ As of 4.3, we're processing Errors thrown from handler methods as well,\n\t\t\t\t\/\/ making them available for @ExceptionHandler methods and other scenarios.\n\t\t\t\tdispatchException = new NestedServletException(\"Handler dispatch failed\", err);\n\t\t\t}\n\t\t\t\/\/ <strong>\u540e\u7eed\u5904\u7406\uff1a\u5904\u7406\u6a21\u578b\u6570\u636e\u548c\u6e32\u67d3\u89c6\u56fe<\/strong>\n\t\t\tprocessDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);\n\t\t}\n\t\tcatch (Exception ex) {\n\t\t\ttriggerAfterCompletion(processedRequest, response, mappedHandler, ex);\n\t\t}\n\t\tcatch (Throwable err) {\n\t\t\ttriggerAfterCompletion(processedRequest, response, mappedHandler,\n\t\t\t\t\tnew NestedServletException(\"Handler processing failed\", err));\n\t\t}\n\t\tfinally {\n\t\t\tif (asyncManager.isConcurrentHandlingStarted()) {\n\t\t\t\t\/\/ Instead of postHandle and afterCompletion\n\t\t\t\tif (mappedHandler != null) {\n\t\t\t\t\tmappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response);\n\t\t\t\t}\n\t\t\t}\n\t\t\telse {\n\t\t\t\t\/\/ Clean up any resources used by a multipart request.\n\t\t\t\tif (multipartRequestParsed) {\n\t\t\t\t\tcleanupMultipart(processedRequest);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">d>processDispatchResult()<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\tprivate void processDispatchResult(HttpServletRequest request, HttpServletResponse response,\r\n\t\t\t@Nullable HandlerExecutionChain mappedHandler, @Nullable ModelAndView mv,\r\n\t\t\t@Nullable Exception exception) throws Exception {\r\n\r\n\t\tboolean errorView = false;\r\n\r\n\t\tif (exception != null) {\r\n\t\t\tif (exception instanceof ModelAndViewDefiningException) {\r\n\t\t\t\tlogger.debug(\"ModelAndViewDefiningException encountered\", exception);\r\n\t\t\t\tmv = ((ModelAndViewDefiningException) exception).getModelAndView();\r\n\t\t\t}\r\n\t\t\telse {\r\n\t\t\t\tObject handler = (mappedHandler != null ? mappedHandler.getHandler() : null);\r\n\t\t\t\tmv = processHandlerException(request, response, handler, exception);\r\n\t\t\t\terrorView = (mv != null);\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\t\/\/ Did the handler return a view to render?\r\n\t\tif (mv != null &amp;&amp; !mv.wasCleared()) {\n\r\t\t\t\/\/ <strong>\u5904\u7406\u6a21\u578b\u6570\u636e\u548c\u6e32\u67d3\u89c6\u56fe<\/strong>\n\t\t\trender(mv, request, response);\r\n\t\t\tif (errorView) {\r\n\t\t\t\tWebUtils.clearErrorRequestAttributes(request);\r\n\t\t\t}\r\n\t\t}\r\n\t\telse {\r\n\t\t\tif (logger.isTraceEnabled()) {\r\n\t\t\t\tlogger.trace(\"No view rendering, null ModelAndView returned.\");\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tif (WebAsyncUtils.getAsyncManager(request).isConcurrentHandlingStarted()) {\r\n\t\t\t\/\/ Concurrent handling started during a forward\r\n\t\t\treturn;\r\n\t\t}\r\n\r\n\t\tif (mappedHandler != null) {\r\n\t\t\t\/\/ Exception (if any) is already handled..\n\r\t\t\t\/\/ <strong>\u8c03\u7528\u62e6\u622a\u5668\u7684afterCompletion()<\/strong>\n\t\t\tmappedHandler.triggerAfterCompletion(request, response, null);\r\n\t\t}\r\n\t}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">4\u3001SpringMVC\u7684\u6267\u884c\u6d41\u7a0b<\/h2>\n\n\n\n<p>1) \u7528\u6237\u5411\u670d\u52a1\u5668\u53d1\u9001\u8bf7\u6c42\uff0c\u8bf7\u6c42\u88abSpringMVC \u524d\u7aef\u63a7\u5236\u5668 DispatcherServlet\u6355\u83b7\u3002<\/p>\n\n\n\n<p>2) DispatcherServlet\u5bf9\u8bf7\u6c42URL\u8fdb\u884c\u89e3\u6790\uff0c\u5f97\u5230\u8bf7\u6c42\u8d44\u6e90\u6807\u8bc6\u7b26\uff08URI\uff09\uff0c\u5224\u65ad\u8bf7\u6c42URI\u5bf9\u5e94\u7684\u6620\u5c04\uff1a<\/p>\n\n\n\n<p>a) \u4e0d\u5b58\u5728<\/p>\n\n\n\n<p>i. \u518d\u5224\u65ad\u662f\u5426\u914d\u7f6e\u4e86mvc:default-servlet-handler<\/p>\n\n\n\n<p>ii. \u5982\u679c\u6ca1\u914d\u7f6e\uff0c\u5219\u63a7\u5236\u53f0\u62a5\u6620\u5c04\u67e5\u627e\u4e0d\u5230\uff0c\u5ba2\u6237\u7aef\u5c55\u793a404\u9519\u8bef<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/qaqaq.top\/wp-content\/uploads\/2022\/12\/image-406.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"988\" height=\"79\" data-original=\"https:\/\/qaqaq.top\/wp-content\/uploads\/2022\/12\/image-406.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-3049\"  sizes=\"(max-width: 988px) 100vw, 988px\" \/><\/div><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/qaqaq.top\/wp-content\/uploads\/2022\/12\/image-407.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"594\" height=\"191\" data-original=\"https:\/\/qaqaq.top\/wp-content\/uploads\/2022\/12\/image-407.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-3050\"  sizes=\"(max-width: 594px) 100vw, 594px\" \/><\/div><\/figure>\n\n\n\n<p>iii. \u5982\u679c\u6709\u914d\u7f6e\uff0c\u5219\u8bbf\u95ee\u76ee\u6807\u8d44\u6e90\uff08\u4e00\u822c\u4e3a\u9759\u6001\u8d44\u6e90\uff0c\u5982\uff1aJS,CSS,HTML\uff09\uff0c\u627e\u4e0d\u5230\u5ba2\u6237\u7aef\u4e5f\u4f1a\u5c55\u793a404<br>\u9519\u8bef<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/qaqaq.top\/wp-content\/uploads\/2022\/12\/image-408-1024x78.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"78\" data-original=\"https:\/\/qaqaq.top\/wp-content\/uploads\/2022\/12\/image-408-1024x78.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-3051\"  sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/div><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/qaqaq.top\/wp-content\/uploads\/2022\/12\/image-409.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"670\" height=\"175\" data-original=\"https:\/\/qaqaq.top\/wp-content\/uploads\/2022\/12\/image-409.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-3052\"  sizes=\"(max-width: 670px) 100vw, 670px\" \/><\/div><\/figure>\n\n\n\n<p>b) \u5b58\u5728\u5219\u6267\u884c\u4e0b\u9762\u7684\u6d41\u7a0b<\/p>\n\n\n\n<p>3) \u6839\u636e\u8be5URI\uff0c\u8c03\u7528HandlerMapping\u83b7\u5f97\u8be5Handler\u914d\u7f6e\u7684\u6240\u6709\u76f8\u5173\u7684\u5bf9\u8c61\uff08\u5305\u62ecHandler\u5bf9\u8c61\u4ee5\u53ca<br>Handler\u5bf9\u8c61\u5bf9\u5e94\u7684\u62e6\u622a\u5668\uff09\uff0c\u6700\u540e\u4ee5HandlerExecutionChain\u6267\u884c\u94fe\u5bf9\u8c61\u7684\u5f62\u5f0f\u8fd4\u56de\u3002<\/p>\n\n\n\n<p>4) DispatcherServlet \u6839\u636e\u83b7\u5f97\u7684Handler\uff0c\u9009\u62e9\u4e00\u4e2a\u5408\u9002\u7684HandlerAdapter\u3002<\/p>\n\n\n\n<p>5) \u5982\u679c\u6210\u529f\u83b7\u5f97HandlerAdapter\uff0c\u6b64\u65f6\u5c06\u5f00\u59cb\u6267\u884c\u62e6\u622a\u5668\u7684preHandler(\u2026)\u65b9\u6cd5\u3010\u6b63\u5411\u3011<\/p>\n\n\n\n<p>6) \u63d0\u53d6Request\u4e2d\u7684\u6a21\u578b\u6570\u636e\uff0c\u586b\u5145Handler\u5165\u53c2\uff0c\u5f00\u59cb\u6267\u884cHandler\uff08Controller)\u65b9\u6cd5\uff0c\u5904\u7406\u8bf7\u6c42\u3002<br>\u5728\u586b\u5145Handler\u7684\u5165\u53c2\u8fc7\u7a0b\u4e2d\uff0c\u6839\u636e\u4f60\u7684\u914d\u7f6e\uff0cSpring\u5c06\u5e2e\u4f60\u505a\u4e00\u4e9b\u989d\u5916\u7684\u5de5\u4f5c\uff1a<\/p>\n\n\n\n<p>a) HttpMessageConveter\uff1a \u5c06\u8bf7\u6c42\u6d88\u606f\uff08\u5982Json\u3001xml\u7b49\u6570\u636e\uff09\u8f6c\u6362\u6210\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5c06\u5bf9\u8c61\u8f6c\u6362\u4e3a\u6307\u5b9a<br>\u7684\u54cd\u5e94\u4fe1\u606f<\/p>\n\n\n\n<p>b) \u6570\u636e\u8f6c\u6362\uff1a\u5bf9\u8bf7\u6c42\u6d88\u606f\u8fdb\u884c\u6570\u636e\u8f6c\u6362\u3002\u5982String\u8f6c\u6362\u6210Integer\u3001Double\u7b49<\/p>\n\n\n\n<p>c) \u6570\u636e\u683c\u5f0f\u5316\uff1a\u5bf9\u8bf7\u6c42\u6d88\u606f\u8fdb\u884c\u6570\u636e\u683c\u5f0f\u5316\u3002 \u5982\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u683c\u5f0f\u5316\u6570\u5b57\u6216\u683c\u5f0f\u5316\u65e5\u671f\u7b49<\/p>\n\n\n\n<p>d) \u6570\u636e\u9a8c\u8bc1\uff1a \u9a8c\u8bc1\u6570\u636e\u7684\u6709\u6548\u6027\uff08\u957f\u5ea6\u3001\u683c\u5f0f\u7b49\uff09\uff0c\u9a8c\u8bc1\u7ed3\u679c\u5b58\u50a8\u5230BindingResult\u6216Error\u4e2d<\/p>\n\n\n\n<p>7) Handler\u6267\u884c\u5b8c\u6210\u540e\uff0c\u5411DispatcherServlet \u8fd4\u56de\u4e00\u4e2aModelAndView\u5bf9\u8c61\u3002<\/p>\n\n\n\n<p>8) \u6b64\u65f6\u5c06\u5f00\u59cb\u6267\u884c\u62e6\u622a\u5668\u7684postHandle(\u2026)\u65b9\u6cd5\u3010\u9006\u5411\u3011\u3002<\/p>\n\n\n\n<p>9) \u6839\u636e\u8fd4\u56de\u7684ModelAndView\uff08\u6b64\u65f6\u4f1a\u5224\u65ad\u662f\u5426\u5b58\u5728\u5f02\u5e38\uff1a\u5982\u679c\u5b58\u5728\u5f02\u5e38\uff0c\u5219\u6267\u884c<br>HandlerExceptionResolver\u8fdb\u884c\u5f02\u5e38\u5904\u7406\uff09\u9009\u62e9\u4e00\u4e2a\u9002\u5408\u7684ViewResolver\u8fdb\u884c\u89c6\u56fe\u89e3\u6790\uff0c\u6839\u636eModel<br>\u548cView\uff0c\u6765\u6e32\u67d3\u89c6\u56fe\u3002<\/p>\n\n\n\n<p>10) \u6e32\u67d3\u89c6\u56fe\u5b8c\u6bd5\u6267\u884c\u62e6\u622a\u5668\u7684afterCompletion(\u2026)\u65b9\u6cd5\u3010\u9006\u5411\u3011\u3002<\/p>\n\n\n\n<p>11) \u5c06\u6e32\u67d3\u7ed3\u679c\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001SpringMVC\u5e38\u7528\u7ec4\u4ef6 DispatcherServlet\uff1a\u524d\u7aef\u63a7\u5236\u5668\uff0c\u4e0d\u9700\u8981\u5de5\u7a0b\u5e08\u5f00\u53d1\uff0c\u7531\u6846\u67b6\u63d0\u4f9b  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[105],"tags":[112],"class_list":["post-3012","post","type-post","status-publish","format-standard","hentry","category-springmvc-code","tag-springmvc"],"_links":{"self":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/3012"}],"collection":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3012"}],"version-history":[{"count":21,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/3012\/revisions"}],"predecessor-version":[{"id":3053,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/3012\/revisions\/3053"}],"wp:attachment":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3012"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3012"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3012"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}