Spring MVC:DispatcherServlet源码结构

public class DispatcherServlet extends FrameworkServlet {
002  
003    /** List of HandlerMappings used by this servlet */
004    private List<HandlerMapping> handlerMappings;
005 
006    /** List of HandlerAdapters used by this servlet */
007    private List<HandlerAdapter> handlerAdapters;
008 
009    /** List of ViewResolvers used by this servlet */
010    private List<ViewResolver> viewResolvers;
011 
012    /**
013     * Exposes the DispatcherServlet-specific request attributes and delegates to {@link #doDispatch}
014     * for the actual dispatching.
015     */
016    @Override
017    protected void doService(HttpServletRequest request, HttpServletResponse response) {
018        doDispatch(request, response);
019    }
020 
021    /**
022     * Process the actual dispatching to the handler.
023     * <p>The handler will be obtained by applying the servlet's HandlerMappings in order.
024     * The HandlerAdapter will be obtained by querying the servlet's installed HandlerAdapters
025     * to find the first that supports the handler class.
026     * <p>All HTTP methods are handled by this method. It's up to HandlerAdapters or handlers
027     * themselves to decide which methods are acceptable.
028     * @param request current HTTP request
029     * @param response current HTTP response
030     * @throws Exception in case of any kind of processing failure
031     */
032    protected void doDispatch(HttpServletRequest request, HttpServletResponse response) {
033        HttpServletRequest processedRequest = request;
034        HandlerExecutionChain mappedHandler = null;
035        ModelAndView mv;
036 
037        // Determine handler for the current request.
038        mappedHandler = getHandler(processedRequest, false);
039 
040        // Determine handler adapter for the current request.
041        HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
042 
043        // Apply preHandle methods of registered interceptors.
044        HandlerInterceptor[] interceptors = mappedHandler.getInterceptors();
045        for (int i = 0; i < interceptors.length; i++) {
046                HandlerInterceptor interceptor = interceptors[i];
047                interceptor.preHandle(processedRequest, response, mappedHandler.getHandler())
048        }
049 
050        // Actually invoke the handler.
051        mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
052 
053        // Apply postHandle methods of registered interceptors.
054        for (int i = interceptors.length - 1; i >= 0; i--) {
055                HandlerInterceptor interceptor = interceptors[i];
056                interceptor.postHandle(processedRequest, response, mappedHandler.getHandler(), mv);
057        }
058 
059        // Did the handler return a view to render?
060        render(mv, processedRequest, response);
061 
062        // Trigger after-completion for successful outcome.
063        triggerAfterCompletion(mappedHandler, interceptorIndex, processedRequest, response, null);
064    }
065 
066  
067    /**
068     * Return the HandlerExecutionChain for this request.
069     * <p>Tries all handler mappings in order.
070     * @param request current HTTP request
071     * @return the HandlerExecutionChain, or <code>null</code> if no handler could be found
072     */
073    protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
074        for (HandlerMapping hm : this.handlerMappings) {
075            HandlerExecutionChain handler = hm.getHandler(request);
076            if (handler != null) {
077                return handler;
078            }
079        }
080        return null;
081    }
082  
083    /**
084     * Return the HandlerAdapter for this handler object.
085     * @param handler the handler object to find an adapter for
086     * @throws ServletException if no HandlerAdapter can be found for the handler. This is a fatal error.
087     */
088    protected HandlerAdapter getHandlerAdapter(Object handler) throws ServletException {
089        for (HandlerAdapter ha : this.handlerAdapters) {
090            if (ha.supports(handler)) {
091                return ha;
092            }
093        }
094    }
095 
096    /**
097     * Render the given ModelAndView.
098     * <p>This is the last stage in handling a request. It may involve resolving the view by name.
099     * @param mv the ModelAndView to render
100     * @param request current HTTP servlet request
101     * @param response current HTTP servlet response
102     * @throws ServletException if view is missing or cannot be resolved
103     * @throws Exception if there's a problem rendering the view
104     */
105    protected void render(ModelAndView mv, HttpServletRequest request, HttpServletResponse response) throws Exception {
106        View view;
107        view = mv.getView();
108        view.render(mv.getModelInternal(), request, response);
109    }
110}
    原文作者:Spring MVC
    原文地址: https://blog.csdn.net/xiaoxing1521025/article/details/8846033
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞