首先,檢查Controller上面是@Controller還是@RestController(兩者區別自行百度)
其次,如下
@GetMapping("/redirect")
public String redirect(RedirectAttributes redirectAttributes)
{
redirectAttributes.addFlashAttribute("test", 1);
return "redirect:/show";
}
@GetMapping("/show")
@ResponseBody
//必須要添加@ModelAttribute標簽,否側將讀不到值
//且必須指定變量名,並不會自動做匹配
public Map<String, Object> show(@ModelAttribute("test") int test)
{
Map<String, Object> modelMap = new HashMap<>();
modelMap.put("String", test);
return modelMap;
}
