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

标签 java spring spring-mvc

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/

相关文章:

unit-testing - 单元测试 Spring MVC web-app : Could not autowire field: private javax. servlet.ServletContext

java - 异常 : java. lang.IndexOutOfBoundsException : Index: 0, 大小:0

Java三元运算符: is behaviour similar to conditional move possible (assembly) upon ART?

spring - 如何在 Spring Controller 中返回纯文本?

java - hibernate5 typeconfiguration 注册表类中的内存泄漏

java - 使用 FileItemStream 流式传输文件时检查实际文件大小

java - 无法找到逻辑名称为 @embeddedid 的列

38k 到 38000 的 java 正则表达式

java - Gradle:如何编译使用 com.sun.xml.internal.bind.* 的类?

java - 如何将mysql数据源添加到applicationContext.xml