1. 程式人生 > 其它 >SpringMVC學習筆記04--------結果跳轉

SpringMVC學習筆記04--------結果跳轉

1. 請求轉發與請求重定向

  1. 請求轉發

    • 一次請求
    • 位址列不會改變
    • 跳轉後的程式碼不會執行
    • 只能在當前專案中轉發
    • 可以傳遞request作用域的資訊
  2. 請求重定向

    • 是兩次請求
    • 位址列會改變
    • 跳轉後的程式碼會執行
    • 可以跳轉到當前伺服器之外的路徑
    • 不能把request作用域資訊傳遞下去

2.SpringMVC實現請求轉發

以下幾種方式的測試我們都採用註解的方式實現,註解相關配置參考SpringMVC學習筆記02-------第一個SpringMVC程式

2.1 使用servletApi(不推薦使用)

  1. 編寫controller類

    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 {
            // 因為要跳轉的jsp頁面在WEB-INF目錄下,請求重定向無法直接訪問到,
            // 我們就先重定向到/servlet/forward,再請求轉發至hello.jsp
            response.sendRedirect("/servlet/forward");
        }
    
    }
    
  2. 配置Tomcat,啟動並測試
    該種方式採用的是原生的servlet的方式實現的,使用起來比較繁瑣,不推薦使用,實際開發中,我們更多的是使用SpringMVC方式進行結果跳轉。

2.2 使用SpringMVC實現結果跳轉(沒有檢視解析器的情形下)

  1. 編寫springMVC的配置檔案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">
    
         <!-- 使得dispatcherServlet不處理靜態資源 -->
         <mvc:default-servlet-handler/>
         <!-- 配置註解驅動,即配置處理器對映器與處理器介面卡 -->
         <mvc:annotation-driven/>
         <!-- 配置掃描包路徑,使得指定包下的註解@Controller生效 -->
         <context:component-scan base-package="com.xdw.controller"/>
    
    
    
         <!-- 配置檢視解析器 -->
     <!--    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="viewResolver">-->
     <!--        <property name="prefix" value="/"/>-->
     <!--        <property name="suffix" value=".jsp"/>-->
     <!--    </bean>-->
    
     </beans>
    
  2. 編寫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() {
            // 請求轉發: 方式一
            return "/index.jsp";
        }
    
        @RequestMapping("/forward02")
        public String testForward02() {
            // 請求轉發: 方式二
            return "forward:/index.jsp";
        }
    
        @RequestMapping("/redirect")
        public String testRedirect() {
            // 請求重定向
            return "redirect:/index.jsp";
        }
    
    }
    
    • 這裡因為我們沒有配置檢視解析器,所以在請求轉發的時候需要寫全需要跳轉的jsp頁面(路徑加上需要跳轉的jsp頁面)。
  3. 啟動Tomcat測試

2.3 使用SpringMVC實現頁面跳轉(配置了檢視解析器)

  1. 編寫SpringMVC配置檔案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">
    
      <!-- 使得dispatcherServlet不處理靜態資源 -->
      <mvc:default-servlet-handler/>
      <!-- 配置註解驅動,即配置處理器對映器與處理器介面卡 -->
      <mvc:annotation-driven/>
      <!-- 配置掃描包路徑,使得指定包下的註解@Controller生效 -->
      <context:component-scan base-package="com.xdw.controller"/>
    
    
    
      <!-- 配置檢視解析器 -->
      <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="viewResolver">
          <property name="prefix" value="/"/>
          <property name="suffix" value=".jsp"/>
      </bean>
    
    </beans>
    
  2. 編寫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() {
              // 請求轉發: 方式一
              return "index";
          }
    
          @RequestMapping("/forward02")
          public String testForward02() {
              // 請求轉發: 方式二
              return "forward:/index.jsp";
          }
    
          @RequestMapping("/redirect")
          public String testRedirect() {
              // 請求重定向
              return "redirect:/index.jsp";
          }
    
      }
    
    • 因為配置了檢視解析器,並設定了檢視解析器的字首與字尾,所以請求轉發可以直接寫成 return "index";,檢視解析器拿到這個之後會自動根據我們的配置找到對應的jsp頁面!
    • 請求重定向本就是發起另一次請求,所以無論有沒有配置檢視解析器,都需要寫全訪問路徑(訪問資源在專案中的路徑 + 資源全稱)!

3.測試