笔记18 客户端跳转

在前面的例子中,无论是/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

猜你喜欢

转载自www.cnblogs.com/lyj-gyq/p/8909956.html