/** * MVC framework SPI interface, allowing parameterization of core MVC workflow. * * <p>Interface that must be implemented for each handler type to handle a request. * This interface is used to allow the {@link DispatcherServlet} to be indefinitely * extensible. The DispatcherServlet accesses all installed handlers through this * interface, meaning that it does not contain code specific to any handler type. * * <p>Note that a handler can be of type <code>Object</code>. This is to enable * handlers from other frameworks to be integrated with this framework without * custom coding, as well as to allow for annotation handler objects that do * not obey any specific Java interface. * * <p>This interface is not intended for application developers. It is available * to handlers who want to develop their own web workflow. * * <p>Note: HandlerAdaptger implementators may implement the * {@link org.springframework.core.Ordered} interface to be able to specify a * sorting order (and thus a priority) for getting applied by DispatcherServlet. * Non-Ordered instances get treated as lowest priority. * * @author Rod Johnson * @author Juergen Hoeller * @see org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter * @see org.springframework.web.servlet.handler.SimpleServletHandlerAdapter */ publicinterfaceHandlerAdapter { /** * Given a handler instance, return whether or not this 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); * </code> * @param handler handler object to check * @return whether or not this object can use the given handler */ //判断当前 HandlerAdapter 是否支持处理 handler booleansupports(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</code> method of this interface, which must have * returned <code>true</code>. * @throws Exception in case of errors * @return ModelAndView object with the name of the view and the required * model data, or <code>null</code> if the request has been handled directly */ //使用 handler 处理请求 ModelAndViewhandle(HttpServletRequest request, HttpServletResponse response, Object handler)throws Exception; /** * Same contract as for HttpServlet's <code>getLastModified</code> 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 */ longgetLastModified(HttpServletRequest request, Object handler); }
/** * Given a handler method, return whether or not this adapter can support it. * @param handlerMethod the handler method to check * @return whether or not this adapter can adapt the given method */ protectedabstractbooleansupportsInternal(HandlerMethod handlerMethod);
/** * This implementation expects the handler to be an {@link HandlerMethod}. */ @Override @Nullable publicfinalModelAndViewhandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
/** * Use the given handler method to handle the request. * @param request current HTTP request * @param response current HTTP response * @param handlerMethod handler method to use. This object must have previously been passed to the * {@link #supportsInternal(HandlerMethod)} this interface, which must have returned {@code true}. * @return a ModelAndView object with the name of the view and the required model data, * or {@code null} if the request has been handled directly * @throws Exception in case of errors */ @Nullable protectedabstractModelAndViewhandleInternal(HttpServletRequest request, HttpServletResponse response, HandlerMethod handlerMethod)throws Exception; }