SpringMVC 实现请求转发和重定向也很方便:
实现请求转发: 🙌
@Controller @RequestMapping("/HelloController") public class HelloController { @GetMapping ("/add/{a}/{b}") public String sayHello(@PathVariable int a, @PathVariable int b, Model model) { model.addAttribute("msg", a + b); return "fofa"; } }
此时请求http://localhost:8080/HelloController/add/1/2
,将会转发到fofa.jsp的页面显示
实现请求重定向: 🛹
@Controller @RequestMapping("/HelloController") public class HelloController { @GetMapping ("/add/{a}/{b}") public String sayHello(@PathVariable int a, @PathVariable int b, Model model) { model.addAttribute("msg", a + b); return "redirect:/index.jsp"; } }
此时请求http://localhost:8080/HelloController/add/1/2
,将会重定向到index.jsp页面