当前位置 : 主页 > 编程语言 > c语言 >

spring mvc3.2 请求及响应过程

来源:互联网 收集:自由互联 发布时间:2023-09-06
doFilter--doFilterInternal--filterChain.doFilter--HttpServlet service(request, response)--doGet(req, resp)--DispatcherServlet service(ServletRequest req, ServletResponse res)--FrameworkServlet.processRequest--doDispatch(request, response)--


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})


【文章原创作者:香港显卡服务器 http://www.558idc.com/hkgpu.html 网络转载请说明出处】
上一篇:编程入门(劝学)
下一篇:没有了
网友评论