如題所示,在SpringMVC中可使用forward和redirect關鍵字在Controller中對原請求進行轉發或重定向到其餘的Controller。好比能夠這樣使用:html
package cn.zifangsky.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; import cn.zifangsky.model.User; @Controller public class TestController { @RequestMapping("/user/index.html") public ModelAndView userIndex() { return new ModelAndView("user/index"); } @RequestMapping("/testForward.html") public ModelAndView testForward(@RequestParam("username") String username){ ModelAndView mAndView = new ModelAndView("forward:/user/index.html"); User user = new User(); user.setName(username); mAndView.addObject("user", user); return mAndView; } @RequestMapping("/testRedirect.html") public ModelAndView testRedirect(@RequestParam("username") String username){ ModelAndView mAndView = new ModelAndView("redirect:/user/index.html"); User user = new User(); user.setName(username); mAndView.addObject("user", user); return mAndView; } }
而後項目啓動後,在瀏覽器中訪問:http://localhost:9180/CookieDemo/testForward.html?username=forwardjava
頁面顯示效果以下:web
能夠看出,在使用forward進行轉發時請求的URL連接是不會改變的spring
接着,在瀏覽器中訪問:http://localhost:9180/CookieDemo/testRedirect.html?username=redirect瀏覽器
頁面顯示效果以下:session
能夠看出,在使用redirect進行重定向時請求的URL連接發生了改變,而且在controller中放置的參數並無傳遞進行。那麼,若是想要在重定向時把參數也傳遞過去應該怎麼作呢?app
示例代碼以下:ide
@RequestMapping("/user/index.html") public ModelAndView userIndex(HttpServletRequest request) { ModelAndView mAndView = new ModelAndView("user/index"); HttpSession session = request.getSession(); mAndView.addObject("user", session.getAttribute("user")); session.removeAttribute("user"); return mAndView; } @RequestMapping("/testRedirect.html") public ModelAndView testRedirect(@RequestParam("username") String username,HttpServletRequest request){ ModelAndView mAndView = new ModelAndView("redirect:/user/index.html"); User user = new User(); user.setName(username); request.getSession().setAttribute("user", user); return mAndView; }
示例代碼以下:spa
@RequestMapping("/user/index.html") public ModelAndView userIndex() { return new ModelAndView("user/index"); } @RequestMapping("/testRedirect.html") public ModelAndView testRedirect(@RequestParam("username") String username,RedirectAttributes redirectAttributes){ ModelAndView mAndView = new ModelAndView("redirect:/user/index.html"); User user = new User(); user.setName(username); // redirectAttributes.addAttribute("user", user); //URL後面拼接參數 redirectAttributes.addFlashAttribute("user", user); return mAndView; }
使用RedirectAttributes這個類來傳遞參數寫法就很簡單了,只須要在須要重定向的controller的方法參數中添加RedirectAttributes類型的參數,而後把須要重定向以後也可以獲取的參數放進去便可orm
固然,聽說RedirectAttributes本質上也是經過Session來實現的(PS:相關源代碼我沒看過,只能聽說了)。實際上上面的代碼是省略的寫法,由於重定向以後就直接返回頁面視圖了,若是通過幾回重定向的話估計上面那種寫法就獲取不到參數了,所以關於獲取參數通常還有如下兩種方式:
(1)使用@ModelAttribute註解獲取參數:
@RequestMapping("/user/index.html") public ModelAndView userIndex(@ModelAttribute("user") User user) { ModelAndView mAndView = new ModelAndView("user/index"); mAndView.addObject("user", user); return mAndView; }
(2)使用RequestContextUtils類來獲取:
@RequestMapping("/user/index.html") public ModelAndView userIndex(HttpServletRequest request) { ModelAndView mAndView = new ModelAndView("user/index"); Map<String, Object> map = (Map<String, Object>) RequestContextUtils.getInputFlashMap(request); User user = (User) map.get("user"); mAndView.addObject("user", user); return mAndView; }
總結,這兩種獲取方式均可以成功獲取到參數。可是仍是略顯麻煩,若是隻是一次重定向以後就返回頁面視圖的話推薦使用最簡單那種寫法
PS:上面圖片中的水印是我我的博客的域名,所以還請管理員手下留情不要給我標爲「轉載文章」,謝謝!!!