Detailed explanation of Springmvc architecture

Framework

The frame structure is as follows:

write picture description here

Architecture Flow

1. The user sends a request to the front-end controller DispatcherServlet
2. The DispatcherServlet receives the request and calls the HandlerMapping handler mapper.
3. The processor mapper finds a specific processor according to the request url, generates a processor object and a processor interceptor (if any) and returns it to the DispatcherServlet.
4. The DispatcherServlet invokes the processor through the HandlerAdapter processor adapter
. 5. Executes the processor (Controller, also called the back-end controller).
6. Controller execution completes and returns to ModelAndView
7. HandlerAdapter returns the controller execution result ModelAndView to DispatcherServlet
8. DispatcherServlet passes ModelAndView to ViewReslover view resolver
9. ViewReslover returns to specific View after parsing
10. DispatcherServlet renders View (that is, fills in model data) into view).
11. DispatcherServlet responds to the user

Component Description

The following components are typically implemented using frameworks:
 DispatcherServlet: Front Controller

When the user request arrives at the front controller, it is equivalent to C in the mvc mode. The dispatcherServlet is the center of the entire process control. It calls other components to process the user's request. The existence of the dispatcherServlet reduces the coupling between the components.

 HandlerMapping: handler mapper

HandlerMapping is responsible for finding the Handler or handler according to the user's request url. Springmvc provides different mappers to implement different mapping methods, such as: configuration file method, implementation interface method, annotation method, etc.

 Handler: Handler (write it yourself)

Handler is the back-end controller following the DispatcherServlet front-end controller. Under the control of DispatcherServlet, Handler processes specific user requests.
Since Handler involves specific user business requests, programmers are generally required to develop Handlers according to business requirements.
write picture description here

 HandlAdapter: handler adapter

The processor is executed through the HandlerAdapter, which is an application of the adapter mode, and more types of processors can be executed by extending the adapter.
The picture below (slightly, too tired to paste..) are many different adapters, all of which can eventually be connected using the usb interface

 ViewResolver: View resolver

The View Resolver is responsible for generating the View view from the processing result. The View Resolver first resolves the logical view name into the physical view name, that is, the specific page address, then generates the View view object, and finally renders the View to display the processing result to the user through the page.

 View: View (write it yourself)

The springmvc framework provides support for many types of View views, including: jstlView, freemarkerView, pdfView, etc. Our most commonly used view is jsp.
Under normal circumstances, it is necessary to display model data to users through page tags or page template technology, and programmers need to develop specific pages according to business requirements.

Description: Among the various components of springmvc , the processor mapper, the processor adapter, and the view resolver are called the three components of springmvc.
The components that need to be developed by the user are handler, view

Components loaded by default

We can use these components without any configuration
because the framework already loads these components by default. The location of the configuration file is as follows:
write picture description here

# Default implementation classes for DispatcherServlet's strategy interfaces.
# Used as fallback when no matching beans are found in the DispatcherServlet context.
# Not meant to be customized by application developers.

org.springframework.web.servlet.LocaleResolver=org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver

org.springframework.web.servlet.ThemeResolver=org.springframework.web.servlet.theme.FixedThemeResolver

org.springframework.web.servlet.HandlerMapping=org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,\
    org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping

org.springframework.web.servlet.HandlerAdapter=org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,\
    org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,\
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter

org.springframework.web.servlet.HandlerExceptionResolver=org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver,\
    org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver,\
    org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver

org.springframework.web.servlet.RequestToViewNameTranslator=org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator

org.springframework.web.servlet.ViewResolver=org.springframework.web.servlet.view.InternalResourceViewResolver

org.springframework.web.servlet.FlashMapManager=org.springframework.web.servlet.support.SessionFlashMapManager

component scanner

Using the component scanner saves the tediousness of configuring each Controller class in the spring container.
Use automatic scanning to mark the controller class of @Controller and
configure it in the springmvc.xml configuration file as follows:

<!-- 配置controller扫描包,多个包之间用,分隔 -->
<context:component-scan base-package="cn.itcast.springmvc.controller" />

Annotation mappers and adapters

Configure Processor Mapper

Annotated handler mapper, which maps methods in the class marked with @ResquestMapping. According to the url defined by @ResquestMapping, match the method marked by @ResquestMapping, and return the HandlerMethod object to the front controller if the match is successful.
The method corresponding to the url is encapsulated in the HandlerMethod object.

Starting from spring 3.1, the use of DefaultAnnotationHandlerMapping has been abolished. It is recommended to use RequestMappingHandlerMapping to complete annotation handler mapping.

Configured in the springmvc.xml configuration file as follows:

<!-- 配置处理器映射器 -->
<bean
    class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />

Annotation description:
@RequestMapping: defines the mapping of request urls to handler function methods

Configure the processor adapter

3.6.2. Configuring handler adapters
Annotated handler adapters adapt methods marked with @ResquestMapping.

Starting from spring 3.1, the use of AnnotationMethodHandlerAdapter has been abolished. It is recommended to use RequestMappingHandlerAdapter to complete annotation handler adaptation.
write picture description here
Configured in the springmvc.xml configuration file as follows:

<!-- 配置处理器适配器 -->
<bean
    class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" />

Annotation-driven

Directly configuring processor mappers and processor adapters is cumbersome and can be loaded using annotation drivers.
SpringMVC uses autoloading RequestMappingHandlerMapping and RequestMappingHandlerAdapter
to use alternative annotation processors and adapter configurations in the springmvc.xml configuration file.

<!-- 注解驱动 -->
<mvc:annotation-driven />

view resolver

The view resolver uses the default InternalResourceViewResolver of the SpringMVC framework. This view resolver supports JSP view resolution and is configured
in the springmvc.xml configuration file as follows:

<!-- Example: prefix="/WEB-INF/jsp/", suffix=".jsp", viewname="test" -> 
        "/WEB-INF/jsp/test.jsp" -->
    <!-- 配置视图解析器 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 配置逻辑视图的前缀 -->
        <property name="prefix" value="/WEB-INF/jsp/" />
        <!-- 配置逻辑视图的后缀 -->
        <property name="suffix" value=".jsp" />
    </bean>

The logical view name needs to be specified by returning ModelAndView in the controller. For example, if the logical view name is ItemList, the final returned jsp view address:
"WEB-INF/jsp/itemList.jsp"

Final jsp physical address: prefix + logical view name + suffix

Modify ItemController

Modify the code that sets the view in the ItemController

// @RequestMapping:里面放的是请求的url,和用户请求的url进行匹配
// action可以写也可以不写
@RequestMapping("/itemList.action")
public ModelAndView queryItemList() {
    // 创建页面需要显示的商品数据
    List<Item> list = new ArrayList<>();
    list.add(new Item(1, "1华为 荣耀8", 2399, new Date(), "质量好!1"));
    list.add(new Item(2, "2华为 荣耀8", 2399, new Date(), "质量好!2"));
    list.add(new Item(3, "3华为 荣耀8", 2399, new Date(), "质量好!3"));
    list.add(new Item(4, "4华为 荣耀8", 2399, new Date(), "质量好!4"));
    list.add(new Item(5, "5华为 荣耀8", 2399, new Date(), "质量好!5"));
    list.add(new Item(6, "6华为 荣耀8", 2399, new Date(), "质量好!6"));

    // 创建ModelAndView,用来存放数据和视图
    ModelAndView modelAndView = new ModelAndView();
    // 设置数据到模型中
    modelAndView.addObject("itemList", list);
    // 设置视图jsp,需要设置视图的物理地址
    // modelAndView.setViewName("/WEB-INF/jsp/itemList.jsp");
    // 配置好视图解析器前缀和后缀,这里只需要设置逻辑视图就可以了。
    // 视图解析器根据前缀+逻辑视图名+后缀拼接出来物理路径
    modelAndView.setViewName("itemList");

    return modelAndView;
}

Effect

The effect is the same as before, as shown below:
write picture description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324772578&siteId=291194637