(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 }
原文地址:https://www.cnblogs.com/fanqisoft/p/10263091.html
时间: 2024-10-11 00:27:55