gpt4 book ai didi

java - 在 spring 中使用模型对象重定向

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:25:37 25 4
gpt4 key购买 nike

我需要将一个对象传递到我要重定向的页面

@RequestMapping(value = "/createNewQuest",method={ RequestMethod.GET, RequestMethod.POST })
public ModelAndView createNewQuest(Quest quest,ModelAndView mav) {
questService.addQuest(quest);


mav.addObject("quest", quest);
mav.setViewName("redirect:/control/displayQuest");
return mav;
}

我的 Controller 类看起来像这样但是 displayQuest 页面没有获取任务对象?

任何帮助将不胜感激..

最佳答案

添加 Spring Flash Attributes处理这种情况:

Annotated controllers typically do not need to work with FlashMap directly. Instead an @RequestMapping method can accept an argument of type RedirectAttributes and use it to add flash attributes for a redirect scenario. Flash attributes added via RedirectAttributes are automatically propagated to the "output" FlashMap. Similarly after the redirect attributes from the "input" FlashMap are automatically added to the Model of the controller serving the target URL.

示例

    @RequestMapping(value = "/createNewQuest",method={ RequestMethod.GET, RequestMethod.POST })
public ModelAndView createNewQuest(@ModelAttribute("quest") Quest quest,
BindingResult binding, ModelAndView mav,
final RedirectAttributes redirectAttributes) {

questService.addQuest(quest);

redirectAttributes.addFlashAttribute("quest", quest);
mav.setViewName("redirect:/control/displayQuest");
return mav;
}

关于java - 在 spring 中使用模型对象重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21905705/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com