码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2014-08-13 10:06:55      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   使用   io   strong   for   

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在跳到页面后马上移除对象。所以你刷新一下后这个值就会丢掉。

 

spring MVC 转发与重定向(传参),布布扣,bubuko.com

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

标签:style   blog   http   color   使用   io   strong   for   

原文地址:http://www.cnblogs.com/cxyj/p/3909131.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!