目錄
- 推薦公衆号
- 引言
- 上一篇
- 正文
-
- 類關系
- 簡介
推薦公衆号
有彩蛋哦!!!(或者公衆号内點選網賺擷取彩蛋)
引言
之前介紹了,來了一個請求之後,DispatcherServlet如何找打需要執行的方法,現在介紹DispatcherServlet如何執行方法
上一篇
SpringMVC(十)-----AbstractHandlerMethodMapping根據request找HandlerMethod
https://blog.csdn.net/yueloveme/article/details/89917049
正文
類關系
簡介
HttpRequestHandlerAdapter 執行HttpRequestHandler的handleRequest方法
SimpleServletHandlerAdapter 執行的是HttpServlet的service方法
RequestMappingHandlerAdapter執行@RequestMapping注解的方法
SimpleControllerHandlerAdapter 執行Controller的handleRequest方法
public interface HandlerAdapter {
/**
* Given a handler instance, return whether or not this {@code HandlerAdapter}
* can support it. Typical HandlerAdapters will base the decision on the handler
* type. HandlerAdapters will usually only support one handler type each.
* <p>A typical implementation:
* <p>{@code
* return (handler instanceof MyHandler);
* }
* @param handler handler object to check
* @return whether or not this object can use the given handler
*/
判斷 是否支援 傳入的 Handler
boolean supports(Object handler);
/**
* Use the given handler to handle this request.
* The workflow that is required may vary widely.
* @param request current HTTP request
* @param response current HTTP response
* @param handler handler to use. This object must have previously been passed
* to the {@code supports} method of this interface, which must have
* returned {@code true}.
* @throws Exception in case of errors
* @return ModelAndView object with the name of the view and the required
* model data, or {@code null} if the request has been handled directly
*/
執行Handler處理請求
@Nullable
ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception;
/**
* Same contract as for HttpServlet's {@code getLastModified} method.
* Can simply return -1 if there's no support in the handler class.
* @param request current HTTP request
* @param handler handler to use
* @return the lastModified value for the given handler
* @see javax.servlet.http.HttpServlet#getLastModified
* @see org.springframework.web.servlet.mvc.LastModified#getLastModified
*/
擷取目前請求的最後更改時間,主要用于供給浏覽器判斷目前請求是否修改過,進而判
斷是否可以直接使用之前緩存的結果
long getLastModified(HttpServletRequest request, Object handler);
}
在DispatcherServlet中的位置
protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
// Determine handler adapter for the current request.
擷取 執行HandlerMethod的HandlerAdapter
HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
// Process last-modified header, if supported by the handler.
String method = request.getMethod();
boolean isGet = "GET".equals(method);
if (isGet || "HEAD".equals(method)) {
long lastModified = ha.getLastModified(request,
mappedHandler.getHandler());
if (logger.isDebugEnabled()) {
logger.debug("Last-Modified value for [" +
getRequestUri(request) + "] is: " + lastModified);
}
if (new ServletWebRequest(request,
response).checkNotModified(lastModified) && isGet) {
return;
}
}
if (!mappedHandler.applyPreHandle(processedRequest,
response)) {
return;
}
執行
// Actually invoke the handler.
mv = ha.handle(processedRequest, response,
mappedHandler.getHandler());
/**
* Return the HandlerAdapter for this handler object.
* @param handler the handler object to find an adapter for
* @throws ServletException if no HandlerAdapter can be found for the handler. This is a fatal error.
*/
擷取 執行HandlerMethod的HandlerAdapter
protected HandlerAdapter getHandlerAdapter(Object handler) throws ServletException {
if (this.handlerAdapters != null) {
for (HandlerAdapter ha : this.handlerAdapters) {
if (logger.isTraceEnabled()) {
logger.trace("Testing handler adapter [" + ha + "]");
}
if (ha.supports(handler)) {
return ha;
}
}
}
throw new ServletException("No adapter for handler [" + handler +
"]: The DispatcherServlet configuration needs to include a HandlerAdapter that supports this handler");
}