您的位置:首页 > 编程语言 > Java开发

SpringMVC 流程(3)-- HandlerAdapter

2015-07-30 10:52 645 查看
在上一篇《SpringMVC 流程(2)-- HandlerMapping》我们知道,DispatcherServlet的流程首先会得到一个HandlerExecutionChain对象,也就是一个拦截器链,那么下一步,就要使用到HandlerAdapter。一. HandlerAdapterPs:首先,最直观的就是从名字就可以看出,使用了一个策略模式(具体策略模式可以自行查找相关资料)。1.1 在HandlerAdapter接口,有一个非常重要的方法handle()方法,该方法会执行我们指定的处理请求的方法,如下:
public interface HandlerAdapter {
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
*/
ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception;
long getLastModified(HttpServletRequest request, Object handler);
 
}
1.2 我们平常使用 SpringMVC 很多时候都是使用注解来标注我们具体的处理方法,因为比较简单方便,其实这也是一种策略。但是,除了这种方式,还有其他的方式,不一定非要用注解,比如说还可以用 the WebFlow FlowExecutor或者 Adobe BlazeDS   MessageBroker这些策略来找到我们具体的处理方法(这两种方式我也暂时不太了解)。那么,HandlerAdapter的就允许我们在这些策略之间切换。这也是HandlerAdapter的最主要的作用。再分析下HandlerMapping和Handler,从我个人理解来看,HandlerMapping相当于找到我们的这个处理类,而HandlerAdapter相当于找到我们具体的方法,并且能够执行handle方法来启动。参考:1.http://www.jpalace.org/document/34/spring-mvc-tutorial#section9
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息