Spring MVC之 HandlerAdapter

Spring MVC之 HandlerAdapter

 

In fact, according to the Controller found by HandlerAdapter, the corresponding method is called, and the corresponding adapter is required for data conversion, data formatting, and data verification.

 

 

HandlerAdapter interface implementation: 

 

HttpRequestHandlerAdapter: Require handler to implement HttpRequestHandler interface,

The methods of this interface are: void handleRequest(HttpServletRequest request, HttpServletResponse response), that is, the handler must have a handleRequest method 

 

SimpleControllerHandlerAdapter: The handler is required to implement the Controller interface. The method of this interface is ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response), which is used in this project. 

 

AnnotationMethodHandlerAdapter: Paired with the DefaultAnnotationHandlerMapping above, it is also outdated 

 

RequestMappingHandlerAdapter: paired with the above RequestMappingHandlerMapping for @RequestMapping 

 

 

 

HandlerAdapter principle

 

In fact, HandlerAdapter is a bunch of standardized interfaces for adaptation. After finding the corresponding adapter, you can connect the Spring framework with our custom controller.

 

 

Code example:

 

Define an Adapter interface 

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

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326573610&siteId=291194637