相关博客:关于RedirectAttributes 重定向带参数请求问题
示例
@PostMapping("/upate/{id}")
public String update(@PathVariable("id")Integer id,Category category,RedirectAttributes attributes) {
int saveRes = categoryService.update(id,category);
if (saveRes == 1) {//成功
attributes.addFlashAttribute("msg", "恭喜,修改成功!");
} else {//失败
attributes.addFlashAttribute("msg", "恭喜,修改失败!");
}
return "redirect:/admin/category/list"; //重定向
}
@GetMapping("/list")
public String list(@ModelAttribute("msg") String msg,Model model) {
PageBean categoryIPage = categoryService.listPage(pageNum, pageSize);
if(msg == null) {
model.addAttribute("msg", "恭喜,查询成功!");
}
model.addAttribute("data", categoryIPage);
return "/admin/category";
}
在list方法中使用@RequestParam:注解不能获取到重定向传递过来的参数,使用@ModelAttribute可以获取数据。