https://www.cnblogs.com/conswin/p/6973528.html
SpringMvc請求方式分為轉發、重定向兩種,是用forward和redirect關鍵字在controller層進行處理。
下面代碼實現了這兩種不同的請求方式:

import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.mvc.support.RedirectAttributes; @Controller public class HelloController { /** * 轉發形式 * @param name * @param model * @return */ @RequestMapping("/helloForward") public String helloForward(@RequestParam(value="name", required=false, defaultValue="World2017") String name, Model model) { model.addAttribute("name", name); return "hello"; } @RequestMapping("/hello") public String hello() { return "hello"; } /** * 使用RedirectAttributes類 * @param name * @param redirectAttributes * @return */ @RequestMapping("/helloRedirect") public String helloRedirect(@RequestParam(value="name", required=false ) String name, RedirectAttributes redirectAttributes) { redirectAttributes.addFlashAttribute("name", name); return "redirect:/hello"; } @RequestMapping("/hello2") public String hello2(Model model,HttpServletRequest request) { HttpSession session = request.getSession(); model.addAttribute("name",session.getAttribute("name")); return "hello"; } /** * 常規做法,重定向之前把參數放進Session中,在重定向之后的controller中把參數從Session中取出並放進ModelAndView * @param name * @param request * @return */ @RequestMapping("/helloRedirect2") public String helloRedirect2(@RequestParam(value="name", required=false ) String name, HttpServletRequest request) { request.getSession().setAttribute("name", name); return "redirect:/hello2"; } }
在使用redirect進行重定向時請求的URL鏈接發生了改變,並且在controller中如果像reward一樣 model.addAttribute("name", name)放置的參數,重定向之后是無法獲取到的,所以重定向需要另外的方式進行參數的傳遞,上面的程序介紹了兩種重定向傳參的方式:
①、重定向之前把參數放進Session中,在重定向之后的controller中把參數從Session中取出並放進ModelAndView
②、使用RedirectAttributes類,這種實現方式比較簡單。
再總結一下servlet中轉發request.getRequestDispatcher().forward()和重定向response.sendRedirect()的區別:
①、轉發是一次請求,一次響應,而重定向是兩次請求,兩次響應
②、轉發:servlet和jsp共享一個request,重定向:兩次請求request獨立,所以前面request里面setAttribute()的任何東西,在后面的request里面都獲取不到
③、轉發:地址欄不會改變,重定向:地址欄發生變化。
SpringMVC中的重定向數據傳遞
https://blog.csdn.net/qq_38449518/article/details/82560347
三、但是!重定向之后客戶端用新請求去訪問主界面,這樣的話剛剛塞到ModelAndView那些屬性不就涼了么。
在這里,我們可以使用上圖的:RedirectAttribute類來保存之前獲得的屬性,這樣就可以避免重定向之后數據丟失的問題。
就是說:原來我們是向ModelAndView中放屬性的,但是現在我們只要往這個類里放就OK了,就可以讓用戶在主界面看到之前想保存的那一堆屬性了。
方法一:使用RequestContextUtils.getInputFlashMap()來直接獲取FlashMap(重點)
我們在Index-Controller中,把數據使用RedirectAttribute.addFlashAttribute()保存在FlashMap中,在重定向的時候,FlashMap中的數據被添加到了Request中,所以使用這個方法可以從Request中獲取到FlashMap。
FlashMap本質上還是一個Map,所以這個方法的返回值是Map<String,?>,我們只要用一個Map來接收就可以了,然后我們就可以提取我們之前存儲下的那些屬性了。(就是可以直接在視圖上使用 ${屬性名} 來獲取)。
方法二、通過(@ModelAttribute(value="屬性名")String xxx)通過屬性名稱獲取屬性值(推薦)
剛剛已經說過了,一開始儲存的數據在重定向的時候都放在Request里了,這些數據被作為初始化的模型(通俗的來講就是這些數據被這個控制器的所有方法共享,即為共享數據),所以通過@ModelAttribute可以在Main-Controller中的任意一個方法里通過屬性名獲取這些屬性。比上面的那個要簡單多了,所以推薦使用這個方法。