Static concept of Spring MVC

Keywords: Spring

A DispatchServlet: front end controller

II Controller: Controller

Three HandlerAdapter: a class used internally by DispatchServlet

Its relationship with DispatchServlet is shown in the following source code:

public class DispatcherServlet extends FrameworkServlet {
    ...
        private void initHandlerAdapters(ApplicationContext context) {
        this.handlerAdapters = null;

        if (this.detectAllHandlerAdapters) {
            // Find all HandlerAdapters in the ApplicationContext, including ancestor contexts.
            Map<String, HandlerAdapter> matchingBeans =
                    BeanFactoryUtils.beansOfTypeIncludingAncestors(context, HandlerAdapter.class, true, false);
            if (!matchingBeans.isEmpty()) {
                this.handlerAdapters = new ArrayList<HandlerAdapter>(matchingBeans.values());
                // We keep HandlerAdapters in sorted order.
                OrderComparator.sort(this.handlerAdapters);
            }
        }
        else {
            try {
                //HandlerAdapter is a class used internally by DispatcherServlet
                HandlerAdapter ha = context.getBean(HANDLER_ADAPTER_BEAN_NAME, HandlerAdapter.class);
                this.handlerAdapters = Collections.singletonList(ha);
            }
            catch (NoSuchBeanDefinitionException ex) {
            }
        }

        // Ensure we have at least some HandlerAdapters, by registering
        // default HandlerAdapters if no other adapters are found.
        if (this.handlerAdapters == null) {
            this.handlerAdapters = getDefaultStrategies(context, HandlerAdapter.class);
            if (logger.isDebugEnabled()) {
                logger.debug("No HandlerAdapters found in servlet '" + getServletName() + "': using default");
            }
        }
    }
    ...
}

The following figure shows the relationship between the above three classes

Four HandlerInterceptor: interceptor, which is an interface

Its source code is as follows:

public interface HandlerInterceptor {
    boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
        throws Exception;

    void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
            throws Exception;

    void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception;
}

stay https://docs.spring.io/spring/docs/5.1.0.RC1/javadoc-api/ You can find its API description

5. HandlerMapping: help DispatchServlet find the appropriate Controller, and use HandlerInterceptor to package the Controller

Its source code is as follows:

public interface HandlerMapping {

    String PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE = HandlerMapping.class.getName() + ".pathWithinHandlerMapping";
    String BEST_MATCHING_PATTERN_ATTRIBUTE = HandlerMapping.class.getName() + ".bestMatchingPattern";
    String INTROSPECT_TYPE_LEVEL_MAPPING = HandlerMapping.class.getName() + ".introspectTypeLevelMapping";
    String URI_TEMPLATE_VARIABLES_ATTRIBUTE = HandlerMapping.class.getName() + ".uriTemplateVariables";
    String MATRIX_VARIABLES_ATTRIBUTE = HandlerMapping.class.getName() + ".matrixVariables";
    String PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE = HandlerMapping.class.getName() + ".producibleMediaTypes";
    HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception;
}

Its function is as follows

6. HandlerExecutionChain

Its function is as follows

Seven Model and view: a concrete representation of Model

Eight view resolver: help DispatchServlet find the right view page

IX View: responsible for page presentation

Ten summary

There are many static concepts. Let's summarize them.

 

Posted by nonexistentera on Sun, 05 Jan 2020 00:41:23 -0800