如何在春天将模型作为重定向属性传递

时间:2018-04-03 08:59:44

标签: java spring spring-mvc

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

当我通过此操作时,我会收到转换异常:无法将list<?>转换为string

1 个答案:

答案 0 :(得分:0)

i)将RedirectAttributes redirectAttributes添加到第一个控制器中的方法参数列表

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)然后,在使用@ModelAttribute注释的第二个控制器使用方法参数中访问重定向属性

@ModelAttribute("userModel") final Object userModel

最后你的两个控制器方法如下: //控制器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";  
        }