@Controller
public class MyController {
@RequestMapping("/doSomething")
public String doSomething(Model model, RedirectAttributes redirectAttributes) {
// 使用 Model 对象向视图添加属性
model.addAttribute("message", "Hello World");
// 使用 RedirectAttributes 向目标 URL 添加参数
redirectAttributes.addAttribute("param1", "value1");
redirectAttributes.addFlashAttribute("flashAttr", "flashValue");
// 执行重定向操作
return "redirect:/targetUrl";
}
}
@Controller
public class MyController {
@RequestMapping("/doSomething")
public String doSomething(@RequestParam("name") String name, RedirectAttributes redirectAttributes) {
// 使用 RedirectAttributes 向目标 URL 添加参数
redirectAttributes.addAttribute("name", name);
// 执行重定向操作
return "redirect:/targetUrl";
}
}
@GetMapping("/targetUrl")
public String targetUrl(Model model) {
// 重定向后model中包含redirectAttributes添加的name参数
// 返回的视图中也可以显示出name参数
return "users";
}