SpringMVC跨重定向請求傳遞數據
阿新 • • 發佈:2019-01-13
redirect null tla ash return url home .get 數據
(1)使用URL模板以路徑變量和查詢參數的形式傳遞數據(一些簡單的數據)
1 @GetMapping("/home/index") 2 public String index(Model model){ 3 Meinv meinv = new Meinv("gaoxing",22); 4 model.addAttribute("lastName",meinv.getLastName()); 5 model.addAttribute("age",meinv.getAge()); 6 return "redirect:/home/details/{lastName}";7 } 8 9 @GetMapping("/home/details/{lastName}") 10 public String details(@PathVariable String lastName, @RequestParam Integer age){ 11 System.out.println(lastName); 12 System.out.println(age); 13 return "home"; 14 }
(2)通過flash屬性發送數據(對象等復雜數據)
1 @GetMapping("/home/index")2 public String index(RedirectAttributes model){ 3 Meinv meinv = new Meinv("gaoxing",22); 4 model.addAttribute("lastName",meinv.getLastName()); 5 model.addFlashAttribute("meinv",meinv); 6 return "redirect:/home/details/{lastName}"; 7 } 8 9 @GetMapping("/home/details/{lastName}")10 public String details(@PathVariable String lastName, Model model){ 11 Meinv meinv = null; 12 if(model.containsAttribute("meinv")){ 13 meinv = (Meinv) model.asMap().get("meinv"); 14 } 15 System.out.println(meinv); 16 return "home"; 17 }
SpringMVC跨重定向請求傳遞數據