SpringMVC_执行流程

news/2024/10/22 9:35:53/

四、SpringMVC执行流程

1.SpringMVC 常用组件

  • DispatcherServlet:前端控制器,用于对请求和响应进行统一处理
  • HandlerMapping:处理器映射器,根据 url/method可以去找到具体的 Handler(Controller)
  • Handler:具体处理器(程序员,以后开发这一部分需要)
  • HandlerAdapter:处理器适配器,进行处理器方法的执行
  • ViewResolver:处理视图相关的

2.处理流程图

在这里插入图片描述

3.执行流程原理分析

3.1DispatcherServlet

  • 初始化操作

    protected void initStrategies(ApplicationContext context) {this.initMultipartResolver(context);this.initLocaleResolver(context);this.initThemeResolver(context);this.initHandlerMappings(context);this.initHandlerAdapters(context);this.initHandlerExceptionResolvers(context);this.initRequestToViewNameTranslator(context);this.initViewResolvers(context);this.initFlashMapManager(context);}
    
  • 具体处理请求的方法

    protected final void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//删掉了一系列没用方法try {//具体执行方法this.doService(request, response);} catch (IOException | ServletException var16) {failureCause = var16;throw var16;} catch (Throwable var17) {failureCause = var17;throw new NestedServletException("Request processing failed", var17);} finally {this.resetContextHolders(request, previousLocaleContext, previousAttributes);if (requestAttributes != null) {requestAttributes.requestCompleted();}this.logResult(request, response, (Throwable)failureCause, asyncManager);this.publishRequestHandledEvent(request, response, startTime, (Throwable)failureCause);}}
    
    protected void doService(HttpServletRequest request, HttpServletResponse response) throws Exception {//删掉一系列干扰代码try {//具体执行this.doDispatch(request, response);} finally {if (!WebAsyncUtils.getAsyncManager(request).isConcurrentHandlingStarted() && attributesSnapshot != null) {this.restoreAttributesAfterInclude(request, attributesSnapshot);}}}
    
    protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {HttpServletRequest processedRequest = request;HandlerExecutionChain mappedHandler = null;boolean multipartRequestParsed = false;WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);try {try {ModelAndView mv = null;Object dispatchException = null;try {processedRequest = this.checkMultipart(request);multipartRequestParsed = processedRequest != request;//获取处理器就是具体的需要执行的 TontrollermappedHandler = this.getHandler(processedRequest);if (mappedHandler == null) {this.noHandlerFound(processedRequest, response);return;}//获取处理器适配器HandlerAdapter ha = this.getHandlerAdapter(mappedHandler.getHandler());String method = request.getMethod();boolean isGet = "GET".equals(method);if (isGet || "HEAD".equals(method)) {long lastModified = ha.getLastModified(request, mappedHandler.getHandler());if ((new ServletWebRequest(request, response)).checkNotModified(lastModified) && isGet) {return;}}if (!mappedHandler.applyPreHandle(processedRequest, response)) {return;}//具体调用 Controller 中的方法mv = ha.handle(processedRequest, response, mappedHandler.getHandler());if (asyncManager.isConcurrentHandlingStarted()) {return;}this.applyDefaultViewName(processedRequest, mv);mappedHandler.applyPostHandle(processedRequest, response, mv);} catch (Exception var20) {dispatchException = var20;} catch (Throwable var21) {dispatchException = new NestedServletException("Handler dispatch failed", var21);}this.processDispatchResult(processedRequest, response, mappedHandler, mv, (Exception)dispatchException);} catch (Exception var22) {this.triggerAfterCompletion(processedRequest, response, mappedHandler, var22);} catch (Throwable var23) {this.triggerAfterCompletion(processedRequest, response, mappedHandler, new NestedServletException("Handler processing failed", var23));}} finally {if (asyncManager.isConcurrentHandlingStarted()) {if (mappedHandler != null) {mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response);}} else if (multipartRequestParsed) {this.cleanupMultipart(processedRequest);}}}
    
    //获取具体执行的
    protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {if (this.handlerMappings != null) {Iterator var2 = this.handlerMappings.iterator();while(var2.hasNext()) {HandlerMapping mapping = (HandlerMapping)var2.next();HandlerExecutionChain handler = mapping.getHandler(request);if (handler != null) {return handler;}}}return null;}
    

3.2HandlerMapping

  • dbug 的图示

    在这里插入图片描述


http://www.ppmy.cn/news/1099115.html

相关文章

pc端字体为什么到12像素以后不生效

pc端字体为什么到12像素以后不生效&#xff1f; 因为谷歌浏览器默认的最小字体是12像素&#xff0c;如果去‘设置’里面修改最小字体&#xff0c;是可以达到修改效果的&#xff0c;但是&#xff0c;你不能解决用户的浏览器设置 解决办法如下 <p class"font">…

谈论浏览器内核

浏览器内核是指浏览器使用的渲染引擎&#xff0c;用于解析并显示网页的内容。主要有以下几种浏览器内核&#xff1a; Trident&#xff08;IE内核&#xff09;&#xff1a;由Microsoft开发&#xff0c;被用于Internet Explorer浏览器。目前已经被Edge取代。 Gecko&#xff1a;…

Linux Java环境变量设置教程

目前Windows 10系统已经很成熟&#xff0c;大多数人开发都在Windows 10系统下进行开发&#xff0c;于是乎我做一下Java环境变量在Windows 10配下的配置&#xff1a; 具体操作如下&#xff1a; JDK下载地址&#xff08;选择合适JDK版本下&#xff09;&#xff1a; JDK10.0.1 …

【C语言】错题本(2)

题目: 将题目代码粘贴在下面便于分析: #define MAX_SIZE AB struct _Record_Struct {unsigned char Env_Alarm_ID : 4;unsigned char Para1 : 2;unsigned char state;unsigned char avail : 1;}*Env_Alarm_Record;struct _Record_Struct *pointer (struct _Record_Struct*)m…

vue: 使用下拉树组件@riophae/vue-treeselect

前言: 在vue中, 因为element-ui 2.X是没有tree-select组件的&#xff0c;到了element-plus就有了 riophae/vue-treeselect是一个基于 Vue.js 的树形选择器组件&#xff0c;可以用于选择树形结构的数据。它支持多选、搜索、异步加载等功能&#xff0c;可以自定义选项的样式和模…

8.1.3 Bit representation and coding - 解读

这段描述定义了一些序列&#xff0c;并规定了它们在编码信息时的使用方式。下面是对每个序列的解析&#xff1a; 1. 序列X&#xff1a;在位持续时间的一半之后&#xff0c;将发生一个“暂停”。这个序列用于表示逻辑“1”。 2. 序列Y&#xff1a;在整个位持续时间内&#xff0c…

栈的压入、弹出序列

⭐️ 题目描述 &#x1f31f; OJ链接&#xff1a;栈的压入、弹出序列 思路&#xff1a; 我们使用一个栈来模拟题目所给的压入、和弹出序列&#xff0c;若模拟成功则是真&#xff0c;模拟失败返回假。我们可以每次先从 pushV 入栈一个元素&#xff0c;在判断当前栈顶元素和 pop…

使用 crontab 定时任务使用 curl 发送请求

crontab 简单用法 crontab 一般是 linux 系统自带的 输入以下命令可以添加定时任务&#xff0c;里面有 crontab 的说明及示例 crontab -e示例格式如下 # 前面五个分别代表分、时、天、月、周&#xff0c;后面就是命令 * * * * * command例如 * * * * * command就是每分钟执行…