HandlerAdapter
是Spring MVC
框架的SPI
(服务提供者接口),用来参数化核心MVC
工作流。
对于处理一个请求的每种handler
类型,都要有一个对应的HandlerAdapter
实现(一个HandlerAdapter
可以支持多种handler
)。该接口的一个目的是让前置DispatcherServlet
可以被无限扩展。因为DispatcherServlet
对handler
的访问可以通过该接口而无需关注每种handler
类型的代码实现细节。
一个handler
可以是任意类型的Object
。这么设计的目的是其他框架的handler
可以不用改造就可以集成到Spring MVC
框架中来,或者是某个handler
对象即使不使用任何Java
接口只要通过注解也能被集成进来。
HandlerAdapter
接口不是给应用开发人员用的。Spring MVC
框架自己提供了一些实现,另外该接口可以用于开发有特定流程要求的handler
。
HandlerAdapter
接口其实只定义了三个方法:
boolean supports(Object handler)
– 是否支持指定的handler
典型实现举例 :
return (handler instanceof MyHandler)
-
ModelAndView handle(HttpServletRequest, HttpServletResponse, Object handler)
– 调用handler
处理请求- 返回一个
ModelAndView
对象,带有view
名称和所需的模型数据 - 如果
handler
处理过程中请求被处理完成,返回结果会是null
- 错误的话会抛出异常
- 返回一个
-
long getLastModified(HttpServletRequest request, Object handler)
- 跟
HttpServlet
的方法getLastModified
语义相同 - 返回资源的
lastModified
信息,handler
根据自己的逻辑定义决定返回什么样的值 - 如果
handler
不支持该语义,可直接返回-1
- 跟
源代码
package org.springframework.web.servlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.lang.Nullable;
/**
* MVC framework SPI, allowing parameterization of the core MVC workflow.
*
* Interface that must be implemented for each handler type to handle a request.
* This interface is used to allow the 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.
*
* Note that a handler can be of type Object. This is to enable
* handlers from other frameworks to be integrated with this framework without
* custom coding, as well as to allow for annotation-driven handler objects that
* do not obey any specific Java interface.
*
* This interface is not intended for application developers. It is available
* to handlers who want to develop their own web workflow.
*
* Note: HandlerAdapter implementors may implement the
* org.springframework.core.Ordered interface to be able to specify a sorting
* order (and thus a priority) for getting applied by the 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
*/
public interface HandlerAdapter {
/**
* 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.
* A typical implementation:
*
* return (handler instanceof MyHandler);
*
* @param handler handler object to check
* @return whether or not this object can use the given 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 supports method of this interface, which must have
* returned true.
* @throws Exception in case of errors
* @return a ModelAndView object with the name of the view and the required
* model data, or null if the request has been handled directly
*/
@Nullable
ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception;
/**
* Same contract as for HttpServlet's 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);
}