上一篇博客springMVC源码分析--HandlerAdapter(一)中我们主要介绍了一下HandlerAdapter接口相关的内容,实现类及其在DispatcherServlet中执行的顺序,接下来我们详细介绍一下其实现类HttpRequestHandlerAdapter,同前几篇博客一样HttpRequestHandlerAdapter的本质还是调用HttpRequestHandler的HttpRequestHandler方法。
supports方法就是判断handler是否是Servlet
getLastModified直接值
handle方法本质是执行HttpRequestHandler.handleRequest方法。
源码如下:
public class HttpRequestHandlerAdapter implements HandlerAdapter { @Override public boolean supports(Object handler) { //判断是否是HttpRequestHandler子类 return (handler instanceof HttpRequestHandler); } @Override public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { //执行HttpRequestHandler的handleRequest方法 ((HttpRequestHandler) handler).handleRequest(request, response); return null; } @Override public long getLastModified(HttpServletRequest request, Object handler) { //返回modified值 if (handler instanceof LastModified) { return ((LastModified) handler).getLastModified(request); } return -1L; } }
本质HttpRequestHandlerAdapter是HttpRequestHandler的适配器,最终调用HttpRequestHandler的handleRequest方法。
接口HttpRequestHandler的实现如下:
public interface HttpRequestHandler { void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException; }