SpringMVC sixth (operation process, integration with Spring and comparison with Struts2)

18. SpringMVC running process

Insert picture description here
There is a corresponding mapping situation:

  • Obtain the HandlerExecutionChain object by HandlerMapping, that is, obtain the HandlerExcutionChain object by RequestMappingHandlerMapping
    Insert picture description here
    Insert picture description here
  • Obtain the HandlerAdapter (RequestMappingHandlerAdapter) object
    Insert picture description here
  • Call the interceptor's PreHandler method
    Insert picture description here
  • Call the target method of the target Handler to get the ModelAndView object
    Insert picture description here
    Insert picture description here
  • Call the interceptor's postHandler method
    Insert picture description here
  • Handling the view.
    Insert picture description here
    If there is an exception, the HandlerExceptionResolver component will handle the exception and get a new ModelAndView object.
    Insert picture description here
    If there is no exception, the ViewResolver component will get the actual View based on the ModelAndView object.
    Insert picture description here
  • Render the view
    Insert picture description here
    Among them, call the InternalResourceView.renderMergedOutputModel() method to forward the request
    Insert picture description here
  • Call the interceptor's afterCompletion method
    Insert picture description here

19. Use SpringMVC in a Spring environment

(1) Does Spring need to integrate SpringMVC? Do I need to add Spring's IOC container? Do I need to configure the ContextLoaderListener that starts the Spring IOC container in the web.xml file?

  • Need: Under normal circumstances, similar to data sources, transactions, and integration of other frameworks are placed in the Spring configuration file (rather than in the SpringMVC configuration file). In fact, Service and Dao are also placed in the IOC container corresponding to the Spring configuration file.

  • No need: all are placed in the SpringMVC configuration file. You can also divide multiple Spring configuration files, and then use the import node to import other configuration files

(2) Problem: If the packages scanned by Spring's IOC container and SpringMVC's IOC container overlap, some beans will be created twice.

(3) Solve:

  • There is no overlap between the packages scanned by Spring's IOC container and SpringMVC's IOC container.
  • Use exclude-filter and include-filter sub-nodes to specify annotations that can only be scanned

beans.xml

	<context:component-scan base-package="com.mycode.springmvc">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
    </context:component-scan>

springmvc.xml

	<context:component-scan base-package="com.mycode.springmvc" use-default-filters="false ">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
    </context:component-scan>

(4) Reference the Bean of the business layer in the Spring MVC configuration file

  • Multiple Spring IOC containers can be set up as a parent-child relationship to achieve good decoupling.

  • The Spring MVC WEB layer container can be used as a sub-container of the "business layer" Spring container: that is, the WEB layer container can refer to the Bean of the business layer container, but the business layer container cannot access the Bean of the WEB layer container (Handler can refer to Service, while Service Handler cannot be referenced)
    Insert picture description here

20, SpringMVC vs. Struts2

(1) The entrance of Spring MVC is Servlet, and Struts2 is Filter

(2) Spring MVC will be slightly faster than Struts2. Spring MVC is based on method design, while Sturts2 is based on class. Each time a request is sent, an Action will be instantiated.

(3) The use of Spring MVC is more concise, and the development efficiency of Spring MVC is indeed higher than struts2: it supports JSR303, and it is more convenient to process ajax requests

(4) Struts2's OGNL expression makes the page development efficiency higher than Spring MVC.

Guess you like

Origin blog.csdn.net/Lu1048728731/article/details/112468451