1.Forward转发
1.1相同controller不同方法
使用forward跳转到相同的控制器,使用springMVC的关键字forward
语法:return “forward:/跳转类上的@RequestMapping的路径/跳转类的方法@RequestMapping路径”
@RequestMapping("/test2")
public String test2(){
System.out.println("same Controller");
return "forward:/forwardRedirest/test1";
}
情况一:第一个controller方法为转发,第二个controller方法为重定向,地址栏发生变化
情况二:第一个controller方法为转发,第二个controller方法为转发,地址栏不发生变化
1.2不同controller不同方法:和相同controller的区别在于跳转类上的@RequestMapping的路径不同
1.3页面
使用转发方式跳转,return页面逻辑名,经过视图解析器
@RequestMapping("/test")
public String test(){
System.out.println("forward");
return "index";
}
2.Redirect重定向
2.1相同controller不同方法,和forward的区别在于关键字不同
使用redirect跳转到相同的controller中不同方法
注意使用SpringMVC提供关键字 redirect
语法:return “redirect:/跳转到@RestMapping/跳转类中方法@RestMapping路径”
情况一:第一个controller方法为重定向,第二个controller方法为重定向,地址栏发生变化,最终为第二个方法重定向地址
情况二:第一个controller方法为重定向,第二个controller方法为转发,地址栏发生变化,最终为第二个方法的地址
@RequestMapping("/test3")
public String test3(){
System.out.println("redirect same contorller");
return "redirect:/forwardRedirest/test";
}
2.2不同controller不同方法:和相同controller的区别在于跳转类上的@RequestMapping的路径不同
2.3页面
使用重定向方式跳转,return使用关键字redirect,不会经过视图解析器
语法:return “redirect:视图全路径” ==> return “redirect: /index.jsp”
@RequestMapping("/test1")
public String test1(){
System.out.println("test1");
return "redirect:/index.jsp";
}
全部代码
package com.progect.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/forwardRedirest")
public class ForwardRedirestController {
// redirect实现不同controller跳转
@RequestMapping("/test5")
public String test5() {
System.out.println("redirect same contorller");
return "redirect:/UserController/test";
}
// forward实现不同controller跳转
@RequestMapping("/test4")
public String test4() {
System.out.println("redirect same contorller");
return "forward:/UserController/test";
}
// 使用redirect跳转到相同的controller中不同方法
// 注意使用SpringMVC提供关键字 redirect
// 语法:return "redirect:/跳转到@RestMapping/跳转类中方法@RestMapping路径"
@RequestMapping("/test3")
public String test3() {
System.out.println("redirect same contorller");
return "redirect:/forwardRedirest/test";
}
// 使用forward跳转到相同的控制器
// 使用springMVC的关键字forward
// 语法:return "forward:/跳转类上的@RestMapping的路径/跳转类的方法@RestMapping路径"
@RequestMapping("/test2")
public String test2() {
System.out.println("forward same Controller");
return "forward:/forwardRedirest/test";
}
// 使用重定向方式跳转,return使用关键字,不会经过视图解析器
// 语法:return "redirect:视图全路径" ==> return "redirect: /index.jsp"
@RequestMapping("test1")
public String test1() {
System.out.println("redirect test1 ");
return "redirect:/index.jsp";
}
@RequestMapping("/test")
public String test() {
System.out.println("forward test");
return "index";
}
}