Redirecting携带数据

来源:互联网 发布:神机妙算软件介绍 编辑:程序博客网 时间:2024/04/28 00:36

通常我们在重定向一个url时,是无法把model携带上的,如


那么有两种方法:

Redirecting with URL templates
Working with flash attributes

(如果你用session保存记得Of course, you’re also responsible forcleaning it up from the session after the redirect.)
来解决这个问题

1.

@RequestMapping(value="/register", method=POST)public String processRegistration(Spitter spitter, RedirectAttributes model) {spitterRepository.save(spitter);model.addAttribute("username", spitter.getUsername());model.addFlashAttribute("spitter", spitter);return "redirect:/spitter/{username}";}
工作原理



为了防止数据意外丢失

@RequestMapping(value="/{username}", method=GET)public String showSpitterProfile(@PathVariable String username, Model model) {if (!model.containsAttribute("spitter")) {model.addAttribute(spitterRepository.findByUsername(username));}return "profile";}



0 0
原创粉丝点击