SpringMVC 流程(3)-- HandlerAdapter

在上一篇《SpringMVC 流程(2)-- HandlerMapping》我们知道,DispatcherServlet的流程首先会得到一个HandlerExecutionChain对象,也就是一个拦截器链,那么下一步,就要使用到HandlerAdapter。

一. HandlerAdapter

Ps:首先,最直观的就是从名字就可以看出,使用了一个策略模式(具体策略模式可以自行查找相关资料)。

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值