springMVC提供的跳转方式的特性: 这两种跳转方式只能跳转资源,`不能跳转页面` 所谓的资源就是指代映射路径 1.forward: 2.redirect: `内部跳转`思想:包括方法的封装,将很多方法中都要完成的功能抽成一个方法并定义映射路径,这样其他映射路径都可以跳转共有路径进行功能完成 内部跳转方式 //服务器内部跳转 @RequestMapping("/forward") @Controller public class ForwardController { @RequestMapping("/test1") public String test1(){ System.out.println("ForwardController test1()..."); return "test"; } //内部跳转方式 //forward:后面跟上映射完整路径 @RequestMapping("/test2") public String test2(){ System.out.println("ForwardController test2()..."); //return "forward:test"; //通过访问 ../forward/test2看能不能跳转到test.jsp,答案:不能 return "forward:/forward/test1";//映射完整命名空间路径 这里面没有相对路径,必须写全 //通过test2内部跳转到test1,再由test1跳转到test.jsp页面 } /* * http://127.0.0.1:9999/springmvc/redirect/test2 * 在浏览器中还是test2的路径,但是界面跳转到了test.jsp,所以叫内部跳转 * *ForwardController test2()... *ForwardController test1()... * */ } 重定向跳转 @RequestMapping("/redirect") @Controller public class RedirectController { @RequestMapping("/test1") public String test1(){ System.out.println("RedirectController test1()..."); return "test"; } @RequestMapping("/test2") public String test2(){ System.out.println("RedirectController test2()..."); return "redirect:/redirect/test1"; //重定向到test1 //test2-->test1-->test.jsp } /* * http://127.0.0.1:9999/springmvc/redirect/test2 * http://127.0.0.1:9999/springmvc/redirect/test1 浏览器界面变成了test1,所以叫重定向 */ }