springmvc 适配器详解[通俗易懂]

2022-08-27 10:28:20 浏览数 (1)

大家好,又见面了,我是你们的朋友全栈君。

大家知道springmvc 是一个非常优良的框架,配置灵活实现简单,只需我们更多的关注我们的业务逻辑。今天我们就通过一个简单的例子模拟适配生成过程。

处理器适配器HandlerAdapter

1、SimpleControllerHandlerAdapter

表示所有实现了org.springframework.web.servlet.mvc.Controller接口的Bean 可以作为Spring Web MVC 中的处理器。

如果需要其他类型的处理器可以通过实现HandlerAdapter 来解决。

案例分析:

(1)模拟一个handlerAdapter

  1. //定义一个Adapter接口
  2. public interface HandlerAdapter {
  3. public boolean supports(Object handler);
  4. public void handle(Object handler);
  5. }
代码语言:javascript复制
//定义一个Adapter接口  
public interface HandlerAdapter {  
    public boolean supports(Object handler);  
    public void handle(Object handler);  
}  

(2)模拟一个Controller

  1. //以下是三种Controller实现
  2. public interface Controller {
  3. }
  4. public class HttpController implements Controller{
  5. public void doHttpHandler(){
  6. System.out.println(“http…”);
  7. }
  8. }
  9. public class SimpleController implements Controller{
  10. public void doSimplerHandler(){
  11. System.out.println(“simple…”);
  12. }
  13. }
  14. public class AnnotationController implements Controller{
  15. public void doAnnotationHandler(){
  16. System.out.println(“annotation…”);
  17. }
  18. }
代码语言:javascript复制
//以下是三种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...");  
    }  
}  

(3)适配器

  1. //下面编写适配器类
  2. public class SimpleHandlerAdapter implements HandlerAdapter {
  3. public void handle(Object handler) {
  4. ((SimpleController)handler).doSimplerHandler();
  5. }
  6. public boolean supports(Object handler) {
  7. return (handler instanceof Controller);
  8. }
  9. }
  10. public class HttpHandlerAdapter implements HandlerAdapter {
  11. public void handle(Object handler) {
  12. ((HttpController)handler).doHttpHandler();
  13. }
  14. public boolean supports(Object handler) {
  15. return (handler instanceof Controller);
  16. }
  17. }
  18. public class AnnotationHandlerAdapter implements HandlerAdapter {
  19. public void handle(Object handler) {
  20. ((AnnotationController)handler).doAnnotationHandler();
  21. }
  22. public boolean supports(Object handler) {
  23. return (handler instanceof Controller);
  24. }
  25. }
代码语言:javascript复制
//下面编写适配器类 
public class SimpleHandlerAdapter implements HandlerAdapter {    
    public void handle(Object handler) {  
        ((SimpleController)handler).doSimplerHandler();  
    }    
    public boolean supports(Object handler) {  
        return (handler instanceof Controller);  
    }    
}  
public class HttpHandlerAdapter implements HandlerAdapter {    
    public void handle(Object handler) {  
        ((HttpController)handler).doHttpHandler();  
    }    
    public boolean supports(Object handler) {  
        return (handler instanceof Controller);  
    }    
}   
public class AnnotationHandlerAdapter implements HandlerAdapter {    
    public void handle(Object handler) {  
        ((AnnotationController)handler).doAnnotationHandler();  
    }    
    public boolean supports(Object handler) {            
        return (handler instanceof Controller);  
    }    
}  

(4)DispatcherServlet

  1. public class Dispatcher {
  2. public static List<HandlerAdapter> handlerAdapters = new ArrayList<HandlerAdapter>();
  3. public DispatchServlet(){
  4. handlerAdapters.add(new AnnotationHandlerAdapter());
  5. handlerAdapters.add(new HttpHandlerAdapter());
  6. handlerAdapters.add(new SimpleHandlerAdapter());
  7. }
  8. public void doDispatch(){
  9. //此处模拟SpringMVC从request取handler的对象,仅仅new出,可以出,
  10. //不论实现何种Controller,适配器总能经过适配以后得到想要的结果
  11. // HttpController controller = new HttpController();
  12. // AnnotationController controller = new AnnotationController();
  13. SimpleController controller = new SimpleController();
  14. //得到对应适配器
  15. HandlerAdapter adapter = getHandler(controller);
  16. //通过适配器执行对应的controller对应方法
  17. adapter.handle(controller);
  18. }
  19. public HandlerAdapter getHandler(Controller controller){
  20. for(HandlerAdapter adapter: this.handlerAdapters){
  21. if(adapter.supports(controller)){
  22. return adapter;
  23. }
  24. }
  25. return null;
  26. }
  27. public static void main(String[] args){
  28. new DispatchServlet().doDispatch();
  29. }
代码语言:javascript复制
public class Dispatcher {        
    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();  
    }        

功能:执行Handler(Controller)

默认走该适配器:

  1. <!– 配置处理器适配器执行Controller ,springmvc默认的SimpleControllerHandlerAdapter:执行Controller–>
  2. <bean class=“org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter”></bean>
代码语言:javascript复制
<!-- 配置处理器适配器执行Controller ,springmvc默认的SimpleControllerHandlerAdapter:执行Controller-->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>

适配器源码:

结合Controller分析

2、HttpRequestHandlerAdapter

HTTP请求处理器适配器

HTTP请求处理器适配器将http请求封装成HttpServletResquest 和HttpServletResponse对象,和servlet接口类似

第一步:配置HttpRequestHandlerAdapter适配器

  1. <!– 配置HttpRequestHandlerAdapter适配器 用来处理实现了HttpRequestHandler后端控制类Controller–>
  2. <bean class=“org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter”></bean>
代码语言:javascript复制
<!-- 配置HttpRequestHandlerAdapter适配器 用来处理实现了HttpRequestHandler后端控制类Controller-->
		<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"></bean>

第二步:编写Controller

  1. public class HttpRequestController implements HttpRequestHandler{
  2. @Override
  3. public void handleRequest(HttpServletRequest request, HttpServletResponse response)
  4. throws ServletException, IOException {
  5. request.setAttribute(“message”, “我是中国人”);
  6. request.getRequestDispatcher(“/WEB-INF/jsps/mess.jsp”).forward(request, response);
  7. }
  8. }
代码语言:javascript复制
public class HttpRequestController implements HttpRequestHandler{
	@Override
	public void handleRequest(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {		
		request.setAttribute("message", "我是中国人");
		request.getRequestDispatcher("/WEB-INF/jsps/mess.jsp").forward(request, response);			
	}
}

第三步:让spring来创建对象Controller,即配置自定义Controller:

  1. <!– 配置自定义的controller –>
  2. <bean name=“/hello.do” class=“controller.HttpRequestController”></bean>
代码语言:javascript复制
<!-- 配置自定义的controller -->
        <bean name="/hello.do" class="controller.HttpRequestController"></bean>

第四步:准备jsp页面

  1. <%@ page language=“java” contentType=“text/html; charset=UTF-8”
  2. pageEncoding=“UTF-8”%>
  3. <!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>
  4. <html>
  5. <head>
  6. <meta http-equiv=“Content-Type” content=“text/html; charset=UTF-8”>
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. <h1>这是我的第二个适配器</h1>
  11. <h1>${message }</h1>
  12. </body>
  13. </html>
代码语言:javascript复制
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>这是我的第二个适配器</h1>
<h1>${message }</h1>
</body>
</html>

第四步:调试运行

总结:适配器可以共存,只是控制器实现不同的接口使用不同的适配器而已。

注意:SimpleControllerHandlerAdapter是默认的适配器。如果使用后面这个适配器必须手动配置。否则将会访问失败。

实现了HttpRequestHandler接口:配置文件里面没有配置这个适配器报如下错误:

No adapter forhandler [springMVC_01.HttpRequestController@12d527f]: Does your handlerimplement a supported interface like Controller?

通过这个错误,我们发现:实现Controller接口是默认的,既是说明:SimpleControllerHandlerAdapter是默认的适配器

还必须注意:实现HttpRequestHandler接口的控制器不支持ControllerClassNameHandlerMapping这个处理器映射器。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/145032.html原文链接:https://javaforall.cn

0 人点赞