SSM-SpringMVC-17:SpringMVC中深度剖析HandlerAdapter处理器适配器底层

释放双眼,带上耳机,听听看~!

 

 ******** ————吾亦无他,唯手熟尔,谦卑若愚,好学若饥————-********

 

 先放一张图

SSM-SpringMVC-17:SpringMVC中深度剖析HandlerAdapter处理器适配器底层

 

 

很熟悉啊,之前就看过,我们之前已经把handlerMapping剖了个底朝天,顺着上次的进度,继续跟,把HandlerAdapter处理器适配器剖一下

 

它同样是由中央调度器DispatcherServlet调度到的,所以,还是这个类中找doDispatch(request,response)这个方法,我把有用的代码,扒下来

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
1protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
2        HttpServletRequest processedRequest = request;
3        HandlerExecutionChain mappedHandler = null;
4        boolean multipartRequestParsed = false;
5        WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
6
7        try {
8            try {
9                ModelAndView err = null;
10                Exception dispatchException = null;
11
12                try {
13                    processedRequest = this.checkMultipart(request);
14                    multipartRequestParsed = processedRequest != request;
15                    mappedHandler = this.getHandler(processedRequest);
16                    if(mappedHandler == null || mappedHandler.getHandler() == null) {
17                        this.noHandlerFound(processedRequest, response);
18                        return;
19                    }
20
21//上面的是我说的HandleMapping的执行 流程
22
23//所以从后面的第一行开始看
24
25
26
27                    HandlerAdapter ex = this.getHandlerAdapter(mappedHandler.getHandler());
28

 

 

 

 


1
2
3
4
5
1HandlerExecutionChain mappedHandler这个变量,是处理器执行链,它的getHandler方法,就是获取它里面的处理器  
2它外面的this.getHandlerAdpter  
3点过去追踪  
4
5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
1    protected HandlerAdapter getHandlerAdapter(Object handler) throws ServletException {
2//迭代器
3        Iterator var2 = this.handlerAdapters.iterator();
4//一会要返回去的适配器对象
5        HandlerAdapter ha;
6        do {
7//如果没有下一个的话。就抛异常
8            if(!var2.hasNext()) {
9                throw new ServletException("No adapter for handler [" + handler + "]: The DispatcherServlet configuration needs to include a HandlerAdapter that supports this handler");
10            }
11//将此强转成为处理器适配器
12            ha = (HandlerAdapter)var2.next();
13            if(this.logger.isTraceEnabled()) {
14                this.logger.trace("Testing handler adapter [" + ha + "]");
15            }
16        } while(!ha.supports(handler));
17//这儿有个适配器模式
18        return ha;
19    }
20

这是下载源码后的最直观的源码

 


1
2
3
4
5
6
7
8
9
10
11
12
13
1    protected HandlerAdapter getHandlerAdapter(Object handler) throws ServletException {
2        for (HandlerAdapter ha : this.handlerAdapters) {
3            if (logger.isTraceEnabled()) {
4                logger.trace("Testing handler adapter [" + ha + "]");
5            }
6            if (ha.supports(handler)) {
7                return ha;
8            }
9        }
10        throw new ServletException("No adapter for handler [" + handler +
11                "]: The DispatcherServlet configuration needs to include a HandlerAdapter that supports this handler");
12    }
13

 

 

看ha.sipports(handler)这个方法

找到HandlerAdapter接口

 


1
2
3
4
5
6
7
8
9
10
1public interface HandlerAdapter {
2
3    boolean supports(Object handler);
4
5    ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception;
6
7    long getLastModified(HttpServletRequest request, Object handler);
8
9}
10

 

 

 

再来看一下自己写的控制器实现了Controller。这样就看DispatcherServlet属性中的HandlerApater谁支持Controller类型的处理器了。在运行的过程中发现SimpleControllerHandlerAdapter是支持Controller类型的控制器的。
来看一下SimpleControllerHandlerAdapter的代码

找它的实现类:Ctrl+H      SimpleControllerHandlerAdapter

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
1public class SimpleControllerHandlerAdapter implements HandlerAdapter {  
2  
3    public boolean supports(Object handler) {  
4        return (handler instanceof Controller);  
5    }  
6  
7    public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler)  
8            throws Exception {  
9  
10        return ((Controller) handler).handleRequest(request, response);  
11    }  
12  
13    public long getLastModified(HttpServletRequest request, Object handler) {  
14        if (handler instanceof LastModified) {  
15            return ((LastModified) handler).getLastModified(request);  
16        }  
17        return -1L;  
18    }  
19  
20}  
21

 

 

它support来判断是否是处理器

拿到处理器在中央调度器一处调用了handle方法

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
1    protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
2        HttpServletRequest processedRequest = request;
3        HandlerExecutionChain mappedHandler = null;
4        boolean multipartRequestParsed = false;
5
6        WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
7
8        try {
9            ModelAndView mv = null;
10            Exception dispatchException = null;
11
12            try {
13                processedRequest = checkMultipart(request);
14                multipartRequestParsed = (processedRequest != request);
15
16                // Determine handler for the current request.
17                mappedHandler = getHandler(processedRequest);
18                if (mappedHandler == null || mappedHandler.getHandler() == null) {
19                    noHandlerFound(processedRequest, response);
20                    return;
21                }
22
23                // Determine handler adapter for the current request.
24                HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
25
26                // Process last-modified header, if supported by the handler.
27                String method = request.getMethod();
28                boolean isGet = "GET".equals(method);
29                if (isGet || "HEAD".equals(method)) {
30                    long lastModified = ha.getLastModified(request, mappedHandler.getHandler());
31                    if (logger.isDebugEnabled()) {
32                        logger.debug("Last-Modified value for [" + getRequestUri(request) + "] is: " + lastModified);
33                    }
34                    if (new ServletWebRequest(request, response).checkNotModified(lastModified) && isGet) {
35                        return;
36                    }
37                }
38
39                if (!mappedHandler.applyPreHandle(processedRequest, response)) {
40                    return;
41                }
42
43                // Actually invoke the handler.
44                mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
45
46//就是上面这处
47

 

看看handle方法,因为被执行调用到,它返回ModelAndView

里面return走的handleRequest是Controller接口的,找它的实现类

AbstractController抽象类,它有实现handleRequest

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
1@Override
2    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
3            throws Exception {
4
5        // Delegate to WebContentGenerator for checking and preparing.
6        checkRequest(request);
7        prepareResponse(response);
8
9        // Execute handleRequestInternal in synchronized block if required.
10        if (this.synchronizeOnSession) {
11            HttpSession session = request.getSession(false);
12            if (session != null) {
13                Object mutex = WebUtils.getSessionMutex(session);
14                synchronized (mutex) {
15                    return handleRequestInternal(request, response);
16                }
17            }
18        }
19
20        return handleRequestInternal(request, response);
21    }
22

 

 

 

 

 

看到最后面的那个返回值了吗?是ModelAndView对象,它调用了handleRequestInternal(request,respone)这个方法

 

还记得AbstractController的实现类吗?不是有种处理器的定义方法就是继承AbstractController****

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
1package cn.dawn.day04abstractController;
2
3import org.springframework.web.servlet.ModelAndView;
4import org.springframework.web.servlet.mvc.AbstractController;
5import org.springframework.web.servlet.mvc.Controller;
6
7import javax.servlet.http.HttpServletRequest;
8import javax.servlet.http.HttpServletResponse;
9
10/**
11 * Created by Dawn on 2018/3/19.
12 */
13//处理器
14public class FirstController extends AbstractController {
15
16    protected ModelAndView handleRequestInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
17        ModelAndView me=new ModelAndView();
18        me.setViewName("index");
19        return me;
20    }
21}
22

 

 

 

它的实现类默认要求必须实现handleRequestInternal(request,response)这个方法

所以,理清了吧,再一步步return回去,最后回到了中央调度器

 

 

 

结论,中央处理器DispatcherServlet通过处理器执行链找到handlerAdapter处理器适配器,后者调度了它的处理器,通过处理器处理,返回一个ModelAndView模型和视图对象,一路返回,到中央调度器


1
2

2

 

给TA打赏
共{{data.count}}人
人已打赏
安全技术

c++ list, vector, map, set 区别与用法比较

2022-1-11 12:36:11

安全漏洞

预警Apache Shiro < 1.6.0 权限绕过漏洞(CVE-2020-13933)

2020-10-16 11:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索