spring mvc controller間跳轉 重定向 傳參
url:http://zghbwjl.blog.163.com/blog/static/12033667220137795252845/
1. 需求背景
需求:spring MVC框架controller間跳轉,需重定向。有幾種情況:不帶參數跳轉,帶參數拼接url形式跳轉,帶參數不拼接參數跳轉,頁面也能顯示。
本來以為挺簡單的一件事情,並且個人認為比較常用的一種方式,一百度全都有了,這些根本不是問題,但是一百度居然出乎我的意料,一堆都不是我想要的結果。無奈啊,自己寫一篇比較全都供以后大家一百度吧,哈哈哈。。。是這些寫的不是很全都人們給了我寫這篇博客的動力。
2. 解決辦法
需求有了肯定是解決辦法了,一一解決,說明下spring的跳轉方式很多很多,我這里只是說一些自我認為好用的,常用的,spring分裝的一些類和方法。
(1)我在后台一個controller跳轉到另一個controller,為什么有這種需求呢,是這樣的。我有一個列表頁面,然后我會進行新增操作,新增在后台完成之后我要跳轉到列表頁面,不需要傳遞參數,列表頁面默認查詢所有的。
方式一:使用ModelAndView
return new ModelAndView("redirect:/toList");
這樣可以重定向到toList這個方法
方式二:返回String
return "redirect:/ toList ";
其它方式:其它方式還有很多,這里不再做介紹了,比如說response等等。這是不帶參數的重定向。
(2)第二種情況,列表頁面有查詢條件,跳轉后我的查詢條件不能丟掉,這樣就需要帶參數的了,帶參數可以拼接url
方式一:自己手動拼接url
new ModelAndView("redirect:/toList?param1="+value1+"¶m2="+value2);
這樣有個弊端,就是傳中文可能會有亂碼問題。
方式二:用RedirectAttributes,這個是發現的一個比較好用的一個類
這里用它的addAttribute方法,這個實際上重定向過去以后你看url,是它自動給你拼了你的url。
使用方法:
attr.addAttribute("param", value);
return "redirect:/namespace/toController";
這樣在toController這個方法中就可以通過獲得參數的方式獲得這個參數,再傳遞到頁面。過去的url還是和方式一一樣的。
(3)帶參數不拼接url頁面也能拿到值(重點是這個)
一般我估計重定向到都想用這種方式:
@RequestMapping("/save")
public String save(@ModelAttribute("form") Bean form,RedirectAttributes attr)
throws Exception {
String code = service.save(form);
if(code.equals("000")){
attr.addFlashAttribute("name", form.getName());
attr.addFlashAttribute("success", "添加成功!");
return "redirect:/index";
}else{
attr.addAttribute("projectName", form.getProjectName());
attr.addAttribute("enviroment", form.getEnviroment());
attr.addFlashAttribute("msg", "添加出錯!錯誤碼為:"+rsp.getCode().getCode()+",錯誤為:"+rsp.getCode().getName());
return "redirect:/maintenance/toAddConfigCenter";
}
}
@RequestMapping("/index")
public String save(@ModelAttribute("form") Bean form,RedirectAttributes attr)
throws Exception {
return "redirect:/main/list";
}
頁面取值不用我說了吧,直接用el表達式就能獲得到,這里的原理是放到session中,session在跳到頁面后馬上移除對象。所以你刷新一下后這個值就會丟掉。
3. 總結
最底層還是兩種跳轉,只是spring又進行了封裝而已,所以說跳轉的方式其實有很多很多種,你自己也可以封一個,也可以用最原始的response來,也沒有問題。好了,就到這兒。
其實也沒有什么,但是知道了這個就很簡單了,之前沒搞懂,現在搞懂了,和大家分享。有問題的給我留言。
spring mvc3中的addFlashAttribute方法
- //第三個參數(UserModel user)默認為綁定對象
- @RequestMapping(value = "/user/save", method = RequestMethod.POST)
- public ModelAndView saveUser(HttpServletRequest request, HttpServletResponse response,UserModel user) throws Exception {
- ModelAndView mv = new ModelAndView("/user/save/result");//默認為forward模式
- // ModelAndView mv = new ModelAndView("redirect:/user/save/result");//redirect模式
- mv.addObject("message","保存用戶成功!");
- return mv;
- }
- @RequestMapping(value = "/user/save", method = RequestMethod.POST)
- public ModelAndView saveUser(UserModel user, RedirectAttributes redirectAttributes) throws Exception {
- redirectAttributes.addFlashAttribute("message", "保存用戶成功!");//使用addFlashAttribute,參數不會出現在url地址欄中
- return "redirect:/user/save/result";
- }
- <form:form id="myform" action="saveUserDetails.action" method="POST" commandName="user">
- <form:input type="text" name="firstName" path="firstName"/>
- <form:input type="text" name="lastName" path="lastName"/>
- <form:input type="text" name="email" path="email"/>
- <input type="submit" value="submit">
- </form:form>
- @RequestMapping(value="/saveUserDetails.action", method=RequestMethod.POST)
- public String greetingsAction(@Validated User user,RedirectAttributesredirectAttributes){
- someUserdetailsService.save(user);
- redirectAttributes.addFlashAttribute("firstName", user.getFirstName());
- redirectAttributes.addFlashAttribute("lastName", user.getLastName())
- return "redirect:success.html";
- }
- success.html:
- <div>
- <h1>Hello ${firstName} ${lastName}. Your details stored in our database.</h1>
- </div><br>
- @RequestMapping(value="/success.html", method=RequestMethod.GET)
- public String successView(HttpServletRequest request){
- Map<String,?> map = RequestContextUtils.getInputFlashMap(request);
- if (map!=null)
- return "success";
- else return "redirect:someOtherView"; //給出其他提示信息
spring mvc 如何請求轉發和重定向呢?
url: http://blog.sina.com.cn/s/blog_9cd9dc7101016abw.html
往下看:
由於這部分內容簡單,一帶而過了。
1.請求轉發:
public ModelAndView testForward(ModelAndView
}
(2)返回字符串
2.請求重定向
(1)帶參數:
p:ignoreDefaultModelOnRedi
(2)無參數
public String testRedirect(){
return "redirect:/index.action";
}