return "forward:index.jsp"; //轉(zhuǎn)發(fā)
return "forward:user.do?method=reg5"; //轉(zhuǎn)發(fā)
return new ModelAndView("/toList");//轉(zhuǎn)發(fā)return "redirect:user.do?method=reg5"; //重定向
return "redirect:http://www.baidu.com"; //重定向
return new ModelAndView("redirect:/toList");//重定向重定向傳參方式一:自己手動(dòng)拼接url
new ModelAndView("redirect:/toList?param1="+value1+"¶m2="+value2); 這樣有個(gè)弊端,就是傳中文可能會(huì)有亂碼問題。
方式二:用RedirectAttributes,這個(gè)是發(fā)現(xiàn)的一個(gè)比較好用的一個(gè)類 這里用它的addAttribute方法,這個(gè)實(shí)際上重定向過去以后你看url,是它自動(dòng)給你拼了你的url。 使用方法:
attr.addAttribute("param", value); return "redirect:/namespace/toController";
方式三:帶參數(shù)不拼接url頁(yè)面也能拿到值(重點(diǎn)是這個(gè)) 一般我估計(jì)重定向到都想用這種方式:
@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"; }頁(yè)面取值直接用el表達(dá)式就能獲得到,這里的原理是放到session中,session在跳到頁(yè)面后馬上移除對(duì)象。所以你刷新一下后這個(gè)值就會(huì)丟掉。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注