1. 程式人生 > >筆記18 客戶端跳轉

筆記18 客戶端跳轉

ping mapping 圖片 測試 編寫 pack public 意思 ret

在前面的例子中,無論是/index跳轉到index.jsp 還是/addProduct 跳轉到showProduct.jsp,都是服務器跳轉。
本例講解如何進行客戶端跳轉

1.修改IndexController

首先映射/jump到jump()方法
在jump()中編寫如下代碼:
ModelAndView mav = new ModelAndView("redirect:/index");

redirect:/index:即表示客戶端跳轉的意思

 1 package controller;
 2 
 3 import javax.servlet.http.HttpServletRequest;
 4 import javax.servlet.http.HttpServletResponse;
5 6 import org.springframework.stereotype.Controller; 7 import org.springframework.web.bind.annotation.RequestMapping; 8 import org.springframework.web.servlet.ModelAndView; 9 10 @Controller 11 public class IndexController { 12 @RequestMapping("/index") 13 public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
14 15 ModelAndView mav = new ModelAndView("index"); 16 mav.addObject("message", "Hello Spring MVC——————客戶端跳轉"); 17 return mav; 18 } 19 20 @RequestMapping("/jump") 21 public ModelAndView jump() { 22 ModelAndView mav = new ModelAndView("redirect:/index"); 23 return
mav; 24 } 25 }

2.測試

訪問頁面:http://localhost:8080/MySpringMVC5/jump

結果客戶端跳轉到了:http://localhost:8080/MySpringMVC5/index

技術分享圖片

筆記18 客戶端跳轉