Spring MVC-Mapping Processor (Part I)

Keywords: Spring xml JSP Java

1. Brief Analysis of Mapping Processor
In spring mvc, using a mapping processor can map web requests to the correct processor. Spring has many mapping processors built in, and
We can also customize the mapping processor. The following examples show two of spring's most commonly used mapping processors:
BeanNameUrlHandler Mapping and SimpleUrlHandler Mapping.
(1) The mapping processor can pass requests to the Handler Execution Chain, and the processor must execute the link.
Contains a processor that can handle the request (essentially, this processor is dynamically added to the processor chain, which can be understood in conjunction with filter working principles), and
Processor links can also contain a series of interceptors.
(2) The two most commonly used spring processors listed above are inherited from the AbstractHandlerMapping class, so they have a parent class.
Sex.
2. Example: BeanNameUrlHandler Mapping
Create the spring MVC_03_handler Mappings project and import the relevant jar packages.
Step 1: Establish the back-end controller MessageController.java, the code is as follows:

package com.asm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class MessageController implements Controller{

    @Override
    public ModelAndView handleRequest(HttpServletRequest arg0,
            HttpServletResponse arg1) throws Exception {
        ModelAndView mav=new ModelAndView("message");
        mav.addObject("message", "Hello, springMVC!");
        return mav;
    }
}

Step 2: Configure web.xml, code as follows:

<servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:springmvc-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>

Step 3: Configure spring mvc-servlet.xml, code as follows:

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
        <property name="prefix" value="/WEB-INF/page/"/>
        <property name="suffix" value=".jsp"/>
</bean>
<bean name="/message.do" class="com.asm.MessageController"/>

Step 4: Create message.jsp in WEB-INF/page directory, the main code is as follows:

  <body>
  Get information in message:
    ${message}
  </body>

Step 5: Start the server and enter... / message.do access test.
Brief analysis of the implementation process:
(1) After starting the server, when we send a message.do request to the server, we are first intercepted by the front-end controller dispatcher Servlet configured in web.xml.
(2) The front-end controller forwards the request to the back-end controller. The process of forwarding is analyzed as follows: when searching for a mapper processor that can execute message.do requests in spring mvc-servlet.xml, it is found that there is no mapper processor that can be processed, then the default mapper processor BeanNameUrlHandlerMapping is used. This back-end controller
The bean Name of Dispatcher Servlet must begin with "/" and be combined with the mapping configuration of Dispatcher Servlet. At the same time, beanName supports wildcard configuration. For example, if matched
Set: When accessing messasge.do, it can also be accessed successfully.
To the MessageController class.
(3) The BeanNameUrlHandler Mapping processor looks for an instance of a bean named "message.do" in the spring container. When it finds the instance, it uses the bean as the back-end controller to process the request, and adds itself to the chain of mapper processors and passes the request to the chain of processors.
(4) The back-end processor processes and returns the view.

Posted by bigswifty on Thu, 04 Jul 2019 17:01:53 -0700