首页 > 其他分享 >spring mvc3.2 请求及响应过程

spring mvc3.2 请求及响应过程

时间:2023-04-25 23:12:54浏览次数:36  
标签:mvc3.2 String -- spring resp req method 响应 METHOD


doFilter-->doFilterInternal-->filterChain.doFilter-->HttpServlet service(request, response)-->doGet(req, resp)-->DispatcherServlet service(ServletRequest req, ServletResponse res)-->FrameworkServlet.processRequest-->doDispatch(request, response)-->DispatcherServlet.doDispatch调用实际action(HandlerAdapter,RequestMappingHandlerAdapter)-->RequestMappingHandlerAdapter.handle-->handleInternal 

 Resolver Handler Processor Discoverer resolve parameter provided Args 



 1,httpServlet 

 protected void service(HttpServletRequest req, HttpServletResponse resp) 

throws ServletException, IOException 

     { 

String method = req.getMethod(); 



if (method.equals(METHOD_GET)) { 

   long lastModified = getLastModified(req); 

   if (lastModified == -1) { 

// servlet doesn't support if-modified-since, no reason 

// to go through further expensive logic 

doGet(req, resp); 

   } else { 

long ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE); 

if (ifModifiedSince < (lastModified / 1000 * 1000)) { 

   // If the servlet mod time is later, call doGet() 

                     // Round down to the nearest second for a proper compare 

                     // A ifModifiedSince of -1 will always be less 

   maybeSetLastModified(resp, lastModified); 

   doGet(req, resp); 

} else { 

   resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED); 

} 

   } 



} else if (method.equals(METHOD_HEAD)) { 

   long lastModified = getLastModified(req); 

   maybeSetLastModified(resp, lastModified); 

   doHead(req, resp); 



} else if (method.equals(METHOD_POST)) { 

   doPost(req, resp); 

    

} else if (method.equals(METHOD_PUT)) { 

   doPut(req, resp); 
  

    

} else if (method.equals(METHOD_DELETE)) { 

   doDelete(req, resp); 

    

} else if (method.equals(METHOD_OPTIONS)) { 

   doOptions(req,resp); 

    

} else if (method.equals(METHOD_TRACE)) { 

   doTrace(req,resp); 

    

} else { 

   // 

   // Note that this means NO servlet supports whatever 

   // method was requested, anywhere on this server. 

   // 



   String errMsg = lStrings.getString("http.method_not_implemented"); 

   Object[] errArgs = new Object[1]; 

   errArgs[0] = method; 

   errMsg = MessageFormat.format(errMsg, errArgs); 

    

   resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, errMsg); 

} 

     }


2,

httpServlet 

     private static final String METHOD_DELETE = "DELETE"; 

     private static final String METHOD_HEAD = "HEAD"; 

     private static final String METHOD_GET = "GET"; 

     private static final String METHOD_OPTIONS = "OPTIONS"; 

     private static final String METHOD_POST = "POST"; 

     private static final String METHOD_PUT = "PUT"; 

     private static final String METHOD_TRACE = "TRACE";


3,DispatcherServlet获取执行的action  

public class DispatcherServlet extends FrameworkServlet 

protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception { 

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; 

}



先获取HandlerExecutionChain
接着获取HandlerInterceptor[] interceptors = mappedHandler.getInterceptors();  for (int i = 0; i < interceptors.length; i++)
紧接着// Actually invoke the handler  mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
获取请求参数HandlerMethodParameter通过PathVariableMethodArgumentResolver eg:NamedValueInfo含有path的contentId
然后:在方法invokeHandlerMethod中创建ModelAndViewContainer并通过request来updateModel
在InvocableHandlerMethod调用实际
处理后在HandlerMethodReturnValueHandlerComposite处理响应,得到响应类型处理类RequestResponseBodyMethodProcessor
然后处理/ Apply postHandle methods of registered interceptors.  --》for (int i = interceptors.length - 1; i >= 0; i--)
渲染数据render(mv, processedRequest, response); view.render(mv.getModelInternal(), request, response);rd.forward出去


如果不是反回json而是view eg:view/workdetail 通过ViewNameMethodReturnValueHandler设置到界面 model.put(bindingResultKey, dataBinder.getBindingResult());  model---> ModelMap   ModelAndView(ModelAndView: reference to view with name 'view/workdetail'; model is {playMovieDTO=com.zqgame.zqyy.dto.PlayMovieDTO@1f60390, careerMap={}, styles=科幻, styleId=1, currentDate=Fri Nov 14 10:09:14 CST 2014, org.springframework.validation.BindingResult.playMovieDTO=org.springframework.validation.BeanPropertyBindingResult: 0 errors})


标签:mvc3.2,String,--,spring,resp,req,method,响应,METHOD
From: https://blog.51cto.com/u_16088628/6225482

相关文章

  • spring 依赖注入用@Autowired报错 No unique bean of type
    1,报错如下Causedby:org.springframework.beans.factory.NoSuchBeanDefinitionException:Nouniquebeanoftype[org.springframework.amqp.rabbit.core.RabbitTemplate]isdefined:expectedsinglematchingbeanbutfound4:[jmsTemplate1,jmsTemplate2,jmsTemplate3......
  • SpringBoot项目使用Junit进行单元测试
    SpringBoot项目使用Junit进行单元测试环境:Springboot2.6.7POM.xml文件添加如下内容:<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency><dependency>&l......
  • springboot自动装配大概原理
    自动装配:pom.xmlspring-boot-dependence:核心都依赖在父类工程中!我们在写入或者引入springboot依赖的时候,不需要指定版,因为有这些仓库的版本启动器:------springboot的启动场景比如spring-boot-starter-web,他就会帮我们导入web环境苏需要的依赖。springboot会将所有的功能......
  • spring mvc3.2 requestbody json显示原理
    1,publicinterfaceHandlerMethodReturnValueHandler{ /** *Whetherthegiven{@linkplainMethodParametermethodreturntype}is *supportedbythishandler. * *@paramreturnTypethemethodreturntypetocheck *@return{@codetrue}ifthis......
  • spring mvc3.2启动分析
    1,GenericServletpublicvoidinit(ServletConfigconfig)throwsServletException{ this.config=config; this.init();}2,HttpServletBean publicfinalvoidinit()throwsServletException{ if(logger.isDebugEnabled()){ logger.debug("......
  • 苹果Mac电脑安装AutoCAD 2024卡死无响应解决方法
    期待已久的AutoCAD2024已经更新了,许多朋友第一时间卸载电脑上的AutoCAD2023,转手下载了最新版的AutoCAD2024,但是安装的时候发现双击包内的InstallAutodeskAutoCAD2024 安装程序后总是会出现程序卡死无响应的情况,不管是重启电脑,还是重新从官网下载安装包都不行。AutoCAD2024......
  • Springboot 在linux后台运行的方法
    1、后台运行程序nohupjava-jar自己的springboot项目.jar>日志文件名.log2>&1&命令详解:nohup:不挂断地运行命令,退出帐户之后继续运行相应的进程。>日志文件名.log:是nohup把command的输出重定向到当前目录的指定的“日志文件名.log”文件中,即输出内容不打印到屏幕上,而......
  • Springboot日期注解失败:while it seems to fit format ‘yyyy-MM-dd‘T‘HH:mm:ss.SSS
    提交字符串到后台映射为Date类型可以加上@DateTimeFormat(pattern="yyyy-MM-ddHH:mm:ss")注解,但是报错了!前端提交字符串到后台,出现如下错误:whileitseemstofitformat'yyyy-MM-dd'T'HH:mm:ss.SSSZ',parsingfails(leniency?null))错误的大致意思就是字符串映射到Da......
  • 使用Dockerfile部署springboot打包jar包
    1、docker下载JDK1.8镜像dockerpulljava:82、编写Dockerfile文件#依赖的父镜像FROMjava:8#作者MAINTAINERdocker-admin#jar包添加到镜像中ADDxxl-job-admin-2.1.2.jarxxl-job-admin.jar#容器暴露的端口即jar程序在容器中运行的端口EXPOSE8080#容器启动之后......
  • java面试题--springboot
    一、SpringBoot自动装配原理是什么?@SpringBootApplication@EnableAutoConfigration\@SpringBootConfigration\@ComponentScan@AutoConfigrationPackage\@ImportMETA-INF\spring.factories二、说一下@Configuration中的属性proxyBeanMethods的作用?首先,引入两个概念:Full全......