SpringMVC重定向和转发

1. forward 转发:地址栏不发生变化。在springmvc中直接转发的jsp页面不能是WEB-INF下面的页面,否者会出现404找不到页面。如果写全路径是可以的但是不好看了。我们可以另外写一个方法:

@RequestMapping("/rest")
    public String test2(Model model){

        model.addAttribute("msg","forward");
        return "forward:/redir";  //转发到另一个方法,另一个方法跳转到web-inf下面的页面
    }
    @RequestMapping("/redir")
    public String redirect(Model model){
        model.addAttribute("msg","forward");
        return  "hello";
    }

2. redirect重定向:和转发一样无法直接重定向到WEB-INF下面的页面,但同样可以间接跳转:

    @RequestMapping("/rest")
    public String test2(Model model){

        model.addAttribute("msg","forward");
        return "redirect:/redir";
    }
    @RequestMapping("/redir")
    public String redirect(Model model){
        model.addAttribute("msg","redirect");
        return  "hello";
    }
原文地址:https://www.cnblogs.com/Difcipo/p/13064020.html