spring MVC 转发与重定向(传参)

return "forward:index.jsp"; //转发 
return "forward:user.do?method=reg5"; //转发
return new ModelAndView("/toList");//转发
return "redirect:user.do?method=reg5"; //重定向
return "redirect:http://www.baidu.com"; //重定向
return new ModelAndView("redirect:/toList"); //重定向

重定向传参

 方式一:自己手动拼接url

                    new ModelAndView("redirect:/toList?param1="+value1+"&param2="+value2);
                    这样有个弊端,就是传中文可能会有乱码问题。

  方式二:用RedirectAttributes,这个是发现的一个比较好用的一个类
                    这里用它的addAttribute方法,这个实际上重定向过去以后你看url,是它自动给你拼了你的url。
                    使用方法:

                     attr.addAttribute("param", value);
                    return "redirect:/namespace/toController";

  方式三:带参数不拼接url页面也能拿到值(重点是这个)
                 一般我估计重定向到都想用这种方式:

    @RequestMapping("/save")
    public String save(@ModelAttribute("form") Bean form,
            RedirectAttributes attr) throws Exception {
        String code = service.save(form);
        attr.addFlashAttribute("name", form.getName());
        attr.addFlashAttribute("success", "添加成功!");
        return "redirect:/index";
    }

    @RequestMapping("/index")
    public String save(@ModelAttribute("form") Bean form,
            RedirectAttributes attr) throws Exception {
        return "redirect:/main/list";
    }

页面取值直接用el表达式就能获得到,这里的原理是放到session中,session在跳到页面后马上移除对象。所以你刷新一下后这个值就会丢掉。

 

转载于:https://www.cnblogs.com/cxyj/p/3909131.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值