五、spingmvc之轉發和重定向 springmvc之helloworld
阿新 • • 發佈:2022-03-14
一般,控制器方法返回字串型別的值會被當做檢視名稱處理(例如springmvc之helloworld 的檢視),但是如果返回的字串中帶forward:或者redirect:字首時,springMVC會對他們進行特殊處理:將forward:和redirect:當成指示符,其後的字串作為URL處理;
一、轉發和重定向的區別:
區別 | 轉發forward() | 重定向sendRedirect() |
---|---|---|
根目錄 | 包含專案訪問地址 | 不包含專案訪問地址 |
位址列 | 不會發生變化(只有第一次的請求地址) | 會發生變化(重定向地址) |
哪裡跳轉 | 伺服器端進行的跳轉 | 瀏覽器端進行的跳轉 |
請求域中資料 | 不會丟失 | 會丟失 |
二、根目錄的區別:
轉 發:僅限於當前web應用內,"/"代表當前web應用的根目錄
重定向:可以定向到任何資源,"/"代表當前web站點的根目錄
例項驗證:
目錄結構和配置方式:
- 同HelloWord的配置方式;
RequestMappingTest:
1 package handler; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 6 @RequestMapping("/springmvc")7 @Controller 8 public class RequestMappingTest { 9 @RequestMapping("/testForword") 10 public String testForword() { 11 System.out.println("testForword"); 12 return "forward:https://www.baidu.com/"; 13 } 14 15 @RequestMapping("/testRedirect") 16 public String testRedirect() {17 System.out.println("testRedirect"); 18 return "redirect:https://www.baidu.com/"; 19 } 20 21 }
index.jsp:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8" isErrorPage="false"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta http-equiv="Content-type" content="text/html;charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <a href="/springmvc/testForword">testForword</a> 11 <br> 12 <a href="/springmvc/testRedirect">testRedirect</a> 13 <br> 14 </body> 15 </html>View Code
success.jsp:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8" isErrorPage="true"%> 3 <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%> 4 <%@ taglib prefix="s" uri="http://www.springframework.org/tags"%> 5 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 6 <!DOCTYPE html> 7 <html> 8 <head> 9 <meta charset="UTF-8"> 10 <title>Insert title here</title> 11 </head> 12 <body> 13 <h4>success成功</h4> 14 <br> 15 </body> 16 </html>View Code
執行結果:
index.jsp 的執行結果如圖,點選testForword 顯示 404且控制檯列印testForword;點選testRedirect,顯示百度首頁且控制檯列印testRedirect;表名重定向可以到任何資源,但是轉發只能轉發當前web資源;
三、位址列&請求域資料&跳轉路徑區別:
例項驗證:
- 配置方式同上
目錄結構:
RequestMappingTest:
package handler; import java.util.Map; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.SessionAttributes; import entity.User; @SessionAttributes(value = { "user" }) @RequestMapping("/springmvc") @Controller public class RequestMappingTest { @RequestMapping("/testForword") public String testForword() { System.out.println("testForword"); return "forward:/helloworld/success"; } @RequestMapping("/testRedirect") public String testRedirect() { System.out.println("testRedirect"); return "redirect:/helloworld/success"; } @ModelAttribute public void getUser(Map<String, Object> map) { User user = new User(); user.setUserName("葉菲菲"); user.setAge(30); map.put("user", user); } }
Hello.java:
1 package handler; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 6 @RequestMapping("/helloworld") 7 @Controller 8 public class Hello { 9 private static final String SUCCESS = "success"; 10 11 @RequestMapping("/success") 12 public String success() { 13 System.out.println("success"); 14 return SUCCESS; 15 } 16 }View Code
index.jsp:同上;
success.jsp:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8" isErrorPage="true"%> 3 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 4 <!DOCTYPE html> 5 <html> 6 <head> 7 <meta charset="UTF-8"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 <h4>success成功</h4> 12 <br> 13 <c:if test="${not empty requestScope.user}"> 14 請求域:${requestScope.user} 15 </c:if> 16 <c:if test="${not empty sessionScope.user}"> 17 session:${sessionScope.user} 18 </c:if> 19 </body> 20 </html>
執行結果:
點選testForword 跳轉到success頁面,顯示sessionScope和requestScope中的user資料;且控制檯列印testForword和success;此時位址列顯示的請求的地址;請求次數只有一次;只有testForword;
點選testRedirect,跳轉到success頁面,顯示sessionScope中的user資料;且控制檯列印testRedirect和success;此時位址列顯示的請求的地址經過轉發的地址;請求次數為兩次;一次是testRedirect,一次是success;
表明:
- 請求域中資料在轉發中不會發生變化,但是 在重定向時會發生改變;
- 位址列的地址 轉發 不會更改 請求地址,重定向 會更改原來的請求地址;
- 請求次數 轉發為一次,重定向為2次;