gpt4 book ai didi

java - 如何在 spring 中将模型作为重定向属性传递

转载 作者:太空宇宙 更新时间:2023-11-04 10:34:38 25 4
gpt4 key购买 nike

redirectModel.addAttribute("Model", model);
return REDIRECT_PREFIX + "../../my-company/organization-management/manage-users";

当我通过这个时,我得到转换异常:无法转换 list<?>string

最佳答案

i) 将RedirectAttributesredirectAttributes添加到第一个 Controller 中的方法参数列表

public String test(
@ModelAttribute("userModel") final Object UserModel,
final BindingResult bindingResult,
final Model model,
final RedirectAttributes redirectAttributes)
ii) Inside the method added code to add flash attribute to redirectAttributes
redirectAttributes.addFlashAttribute("userModel", mapping1FormObject);

iii) 然后,在第二个 Controller 中使用@ModelAttribute注释的方法参数来访问重定向属性

@ModelAttribute("userModel") final Object userModel

最后你的两个 Controller 方法如下所示:// Controller 1

@RequestMapping(value = { "/page1" }, method = RequestMethod.POST)
public String test1(
@ModelAttribute("userModel") final Object userModel,
final BindingResult bindingResult,
final Model model,
final RedirectAttributes redirectAttributes) {

redirectAttributes.addFlashAttribute("userModel", mapping1FormObject);

return "redirect:page2";
}
//Controller2


@RequestMapping(value = "/page2", method = RequestMethod.GET)
public String test2(
@ModelAttribute("userModel") final Object userModel,
final BindingResult bindingResult,
final Model model) {


return "new/view";
}

关于java - 如何在 spring 中将模型作为重定向属性传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49626038/

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