参考入门案例工程:Spring MVC入门案例!!!-CSDN博客
首先在index.jsp文件中写两个超链接进行页面跳转:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<br><a href="/account/findAccount3">重定向</a>
<br><a href="/account/findAccount5">请求转发</a>
</html>
1.重定向:在AccountController中定义两个方法,进行重定向。
@Controller
@RequestMapping("/account")
public class AccountController {
@GetMapping("/findAccount3")
public String findAccount3() {
return "redirect:/account/findAccount4";
}
@GetMapping("/findAccount4")
public String findAccount4(Model model) {
model.addAttribute("msg", "重定向");
return "success";
}
}
2.转发:在AccountController中定义两个方法,进行转发。
@Controller
@RequestMapping("/account")
public class AccountController {
@GetMapping("/findAccount5")
public String findAccount5() {
return "forward:/account/findAccount6";
}
@GetMapping("/findAccount6")
public String findAccount6(Model model) {
model.addAttribute("msg", "请求转发");
return "success";
}
}