SpringMVC learning notes 04 ------- result jump

1. Request forwarding and request redirection

  1. Request forwarding

    • One request
    • The address bar will not change
    • The code after jump will not be executed
    • Can only be forwarded in the current project
    • Can pass the information of request scope
  2. request redirections

    • Two requests
    • The address bar will change
    • The code after the jump will be executed
    • You can jump to a path outside the current server
    • The request scope information cannot be passed on

2. Spring MVC realizes request forwarding

The following tests are implemented by annotation, and the relevant configuration reference is annotated Spring MVC learning notes 02 ------ the first spring MVC program

2.1 using servletApi (not recommended)

  1. Write the controller class

    package com.xdw.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    @Controller
    @RequestMapping("/servlet")
    public class ServletApiController {
    
        @RequestMapping("/forward")
        public void testForward(HttpServletRequest request, HttpServletResponse response) throws Exception {
            request.setAttribute("msg", "I am forward!");
            request.getRequestDispatcher("/WEB-INF/jsp/hello.jsp").forward(request, response);
        }
    
        @RequestMapping("/redirect")
        public void testRedirect(HttpServletRequest request, HttpServletResponse response) throws Exception {
            // Because the jsp page to jump is in the WEB-INF directory, the request redirection cannot be accessed directly,
            // We redirect to / servlet/forward first, and then forward the request to hello jsp
            response.sendRedirect("/servlet/forward");
        }
    
    }
    
  2. Configure Tomcat, start and test
    This method is implemented in the way of native servlet, which is cumbersome to use and is not recommended. In actual development, we mostly use spring MVC to jump the results.

2.2 using spring MVC to realize result jump (without view parser)

  1. Write the spring MVC configuration file ApplicationContext xml

     <?xml version="1.0" encoding="UTF-8"?>
     <beans xmlns="http://www.springframework.org/schema/beans"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
            xmlns:context="http://www.springframework.org/schema/context"
            xsi:schemaLocation="http://www.springframework.org/schema/beans
             https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    
         <!-- bring dispatcherServlet Do not process static resources -->
         <mvc:default-servlet-handler/>
         <!-- Configure annotation driver, that is, configure processor mapper and processor adapter -->
         <mvc:annotation-driven/>
         <!-- Configure the scanning package path to specify the annotation under the package@Controller take effect -->
         <context:component-scan base-package="com.xdw.controller"/>
    
    
    
         <!-- Configure view parser -->
     <!--    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="viewResolver">-->
     <!--        <property name="prefix" value="/"/>-->
     <!--        <property name="suffix" value=".jsp"/>-->
     <!--    </bean>-->
    
     </beans>
    
  2. Write Controller

    package com.xdw.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    @RequestMapping("/springMVC")
    public class MvcNoVrController {
    
        @RequestMapping("/forward01")
        public String testForward01() {
            // Request forwarding: Method 1
            return "/index.jsp";
        }
    
        @RequestMapping("/forward02")
        public String testForward02() {
            // Request forwarding: mode 2
            return "forward:/index.jsp";
        }
    
        @RequestMapping("/redirect")
        public String testRedirect() {
            // request redirections 
            return "redirect:/index.jsp";
        }
    
    }
    
    • Here, because we have not configured the view parser, we need to write all the jsp pages that need to jump (path plus jsp pages that need to jump) when forwarding the request.
  3. Start Tomcat test

2.3 using spring MVC to realize page Jump (configured with view parser)

  1. Write the spring MVC configuration file ApplicationContext xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
         xmlns:context="http://www.springframework.org/schema/context"
         xsi:schemaLocation="http://www.springframework.org/schema/beans
          https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    
      <!-- bring dispatcherServlet Do not process static resources -->
      <mvc:default-servlet-handler/>
      <!-- Configure annotation driver, that is, configure processor mapper and processor adapter -->
      <mvc:annotation-driven/>
      <!-- Configure the scanning package path to specify the annotation under the package@Controller take effect -->
      <context:component-scan base-package="com.xdw.controller"/>
    
    
    
      <!-- Configure view parser -->
      <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="viewResolver">
          <property name="prefix" value="/"/>
          <property name="suffix" value=".jsp"/>
      </bean>
    
    </beans>
    
  2. Write controller

      package com.xdw.controller;
      import org.springframework.stereotype.Controller;
      import org.springframework.web.bind.annotation.RequestMapping;
    
      @Controller
      @RequestMapping("/springMVC02")
      public class MvcController {
    
          @RequestMapping("/forward01")
          public String testForward01() {
              // Request forwarding: Method 1
              return "index";
          }
    
          @RequestMapping("/forward02")
          public String testForward02() {
              // Request forwarding: mode 2
              return "forward:/index.jsp";
          }
    
          @RequestMapping("/redirect")
          public String testRedirect() {
              // request redirections 
              return "redirect:/index.jsp";
          }
    
      }
    
    • Because the view parser is configured and the prefix and suffix of the view parser are set, the request forwarding can be directly written as return "index";, After the view parser gets this, it will automatically find the corresponding jsp page according to our configuration!
    • Request redirection is to initiate another request, so no matter whether the view parser is configured or not, you need to write the full access path (the path to access resources in the project + the full name of resources)!

3. Test

Keywords: Spring MVC

Added by busyguy78 on Wed, 26 Jan 2022 11:53:01 +0200