Spring MVC之 HandlerAdapter
Spring MVC之 HandlerAdapter
实际上就是根据HandlerAdapter找到的Controller,调用相应的方法,其中数据转换、数据格式化、数据验证就需要相应的适配器去做。
HandlerAdapter 接口实现:
HttpRequestHandlerAdapter : 要求handler实现HttpRequestHandler接口,
该接口的方法为: void handleRequest(HttpServletRequest request, HttpServletResponse response)也就是 handler必须有一个handleRequest方法
SimpleControllerHandlerAdapter:要求handler实现Controller接口,该接口的方法为ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response),也就是本工程采用的
AnnotationMethodHandlerAdapter:和上面的DefaultAnnotationHandlerMapping配对使用的,也已过时
RequestMappingHandlerAdapter:和上面的RequestMappingHandlerMapping配对使用,针对@RequestMapping
HandlerAdapter原理
其实HandlerAdapter就是一堆适配的规范接口,找到对应的适配器后,就能将Spring框架和我们自定义的controller连接起来。
代码例子:
定义一个Adapter接口
public interface HandlerAdapter { public boolean supports(Object handler); public void handle(Object handler); }
以下是三种Controller实现
public interface Controller { } public class HttpController implements Controller{ public void doHttpHandler(){ System.out.println("http..."); } } public class SimpleController implements Controller{ public void doSimplerHandler(){ System.out.println("simple..."); } } public class AnnotationController implements Controller{ public void doAnnotationHandler(){ System.out.println("annotation..."); } }
下面编写适配器类
public class SimpleHandlerAdapter implements HandlerAdapter { public void handle(Object handler) { ((SimpleController)handler).doSimplerHandler(); } public boolean supports(Object handler) { return (handler instanceof SimpleController); } } public class HttpHandlerAdapter implements HandlerAdapter { public void handle(Object handler) { ((HttpController)handler).doHttpHandler(); } public boolean supports(Object handler) { return (handler instanceof HttpController); } } public class AnnotationHandlerAdapter implements HandlerAdapter { public void handle(Object handler) { ((AnnotationController)handler).doAnnotationHandler(); } public boolean supports(Object handler) { return (handler instanceof AnnotationController); } }
模拟一个DispatcherServlet
import java.util.ArrayList; import java.util.List; public class DispatchServlet { public static List<HandlerAdapter> handlerAdapters = new ArrayList<HandlerAdapter>(); public DispatchServlet(){ handlerAdapters.add(new AnnotationHandlerAdapter()); handlerAdapters.add(new HttpHandlerAdapter()); handlerAdapters.add(new SimpleHandlerAdapter()); } public void doDispatch(){ //此处模拟SpringMVC从request取handler的对象,仅仅new出,可以出, //不论实现何种Controller,适配器总能经过适配以后得到想要的结果 // HttpController controller = new HttpController(); // AnnotationController controller = new AnnotationController(); SimpleController controller = new SimpleController(); //得到对应适配器 HandlerAdapter adapter = getHandler(controller); //通过适配器执行对应的controller对应方法 adapter.handle(controller); } public HandlerAdapter getHandler(Controller controller){ for(HandlerAdapter adapter: this.handlerAdapters){ if(adapter.supports(controller)){ return adapter; } } return null; } public static void main(String[] args){ new DispatchServlet().doDispatch(); } }
参考:
https://zhidao.baidu.com/question/136290865875605525.html