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";
    }
复制代码

 

posted on   Difcipo  阅读(229)  评论(0编辑  收藏  举报

努力加载评论中...
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

导航

统计

点击右上角即可分享
微信分享提示