[SpringMVC] Overview of 9 major components

[9] SpringMVC large component overview
original good life _ Last post number 12994 on 2017-06-23 15:48:47 read collection
expanded
    SpringMVC Servlet in a total of three levels, namely the HttpServletBean, FrameworkServlet and DispatcherServlet.
    HttpServletBean inherits directly from java's HttpServlet, and its role is to set the parameters configured in the Servlet to the corresponding properties;
    FrameworkServlet initializes the WebApplicationContext, DispatcherServlet initializes its own 9 components. The content of this blog is an overview of 9 components.
    Before learning 9 components, we need to understand the concept of Handler, that is, the processor. It directly responds to the C layer in MVC, which is the Controller layer. There are many specific forms of expression, which can be classes or methods. All methods marked by @RequestMapping in the Controller layer can be regarded as a Handler, as long as it can actually handle the request.
    The concept of Handler is clear, and the 9 components are introduced one by one below.
[1. HandlerMapping]

    It is used to find Handler. There will be many requests in Spring MVC, each request needs a Handler to process, which Handler is used to process after receiving a specific request? This is what HandlerMapping needs to do.
[2. HandlerAdapter]

    From the name, it is an adapter. Because the Handler in SpringMVC can be in any form, as long as it can process the request, it is ok, but the structure of the processing method required by the Servlet is fixed, and both take the request and response as parameters. How to make the fixed Servlet processing method call flexible Handler for processing? This is what the HandlerAdapter does.
    Summary: Handler is a tool used to work; HandlerMapping is used to find the corresponding tool according to the work required; HandlerAdapter is a person who uses the tool to work.
[3. HandlerExceptionResolver]

    Other components are used to work. Problems will inevitably occur during the work, what should I do if there is a problem? This requires a special role to handle exceptions, in SpringMVC is HandlerExceptionResolver. Specifically, the role of this component is to set ModelAndView according to the exception, and then give it to the render method for rendering.
[4. ViewResolver]

    ViewResolver is used to resolve the view name of String type and Locale to the view of View type. View is used to render the page, that is, the parameters returned by the program are filled into the template to generate html (may be other types) files. There are two key questions here: which template to use? What technique (rule) is used to fill in the parameters? This is actually the main work of ViewResolver. ViewResolver needs to find the template and technology used for rendering (that is, the type of view) to render. The specific rendering process is left to different views to complete.
[5. RequestToViewNameTranslator]

    ViewName is to find View according to ViewName, but some Handlers do not set View or ViewName after processing. At this time, you need to get ViewName from request. How to get ViewName from request is what RequestToViewNameTranslator has to do. RequestToViewNameTranslator can only be configured in the Spring MVC container, so all the conversion rules from request to ViewName must be implemented in a Translator.
[6. LocaleResolver]

    Two parameters are required to resolve the view: one is the view name and the other is Locale. The view name is returned by the processor. Where does Locale come from? This is what LocaleResolver does. LocaleResolver is used to parse out the Locale from the request. Locale is zh-cn or the like, which represents a region. With this, you can display different results to users in different regions. SpringMVC mainly uses Locale in two places: one is when ViewResolver is used for view resolution; the other is when it uses internationalized resources or themes.
[7. ThemeResolver]

    Used to parse the theme. A theme in SpringMVC corresponds to a properties file, which stores all resources related to the current theme, such as pictures and CSS styles. The theme of SpringMVC also supports internationalization, and different areas of the same theme can also display different styles. ThemeResolver, ThemeSource and Theme are related to the theme in SpringMVC. The theme is embodied through a series of resources. To get the resources of a theme, you must first get the name of the resource. This is the work of ThemeResolver. Then find the corresponding theme (can be understood as a configuration) file by the theme name, which is the work of ThemeSource. Finally, just get resources from the theme.
[8. MultipartResolver]

    Used to process upload requests. The processing method is to wrap the ordinary request into MultipartHttpServletRequest, which can directly call the getFile method to obtain the File. If you upload multiple files, you can also call getFileMap to get the Map of the FileName-> File structure. There are three methods in this component, which are used to determine whether it is an upload request, wrap the request into a MultipartHttpServletRequest, and clean up the temporary resources generated during the upload process after processing.
[9. FlashMapManager]

    Used to manage FlashMap, FlashMap is mainly used to transfer parameters in redirect.
【to sum up】

    So far, the nine major components in SpringMVC are briefly outlined. Through the macroscopic understanding of these 9 major components, it will be of great help to analyze the design, principle and implementation of SpringMVC.
————————————————
Copyright Statement: This article is an original article of CSDN blogger "Good Life_", follow the CC 4.0 BY-SA copyright agreement, please attach the original source link and reprint This statement.
Original link: https://blog.csdn.net/hu_zhiting/article/details/73648939

Guess you like

Origin www.cnblogs.com/fangdie/p/12733084.html