【问题标题】:How to pass model attributes from one Spring MVC controller to another controller?如何将模型属性从一个 Spring MVC 控制器传递到另一个控制器?
【发布时间】:2011-11-17 18:46:58
【问题描述】:

我正在从一个控制器重定向到另一个控制器。但我还需要将模型属性传递给第二个控制器。

我不想将模型放入会话中。

请帮忙。

【问题讨论】:

  • 考虑接受最受好评的答案(@aborskiy)。自 2013 年以来,这似乎是正确的解决方案。

标签: redirect spring-mvc controllers modelattribute


【解决方案1】:

我使用的是 spring 3.2.3,这是我解决类似问题的方法。
1) 在控制器1的方法参数列表中添加了RedirectAttributes redirectAttributes。

public String controlMapping1(
        @ModelAttribute("mapping1Form") final Object mapping1FormObject,
        final BindingResult mapping1BindingResult,
        final Model model, 
        final RedirectAttributes redirectAttributes)

2) 在方法内部添加代码以将flash属性添加到redirectAttributes redirectAttributes.addFlashAttribute("mapping1Form", mapping1FormObject);

3) 然后,在第二个控制器中使用带有@ModelAttribute 注解的方法参数来访问重定向属性

@ModelAttribute("mapping1Form") final Object mapping1FormObject

这是来自控制器 1 的示例代码:

@RequestMapping(value = { "/mapping1" }, method = RequestMethod.POST)
public String controlMapping1(
        @ModelAttribute("mapping1Form") final Object mapping1FormObject,
        final BindingResult mapping1BindingResult,
        final Model model, 
        final RedirectAttributes redirectAttributes) {

    redirectAttributes.addFlashAttribute("mapping1Form", mapping1FormObject);

    return "redirect:mapping2";
}   

来自控制器 2:

@RequestMapping(value = "/mapping2", method = RequestMethod.GET)
public String controlMapping2(
        @ModelAttribute("mapping1Form") final Object mapping1FormObject,
        final BindingResult mapping1BindingResult,
        final Model model) {

    model.addAttribute("transformationForm", mapping1FormObject);

    return "new/view";  
}

【讨论】:

  • 回答得很好,正是我想要的。
  • 也适用于我的设置!谢谢!另外你的解释很清楚:)
  • 很棒的解释。刚得到我想要的。但是参数是附加到url的,我们可以在不附加到url的情况下限制参数吗?
  • 重新插入有误导性。请参阅@petr-kopac 回复。
  • 请注意...因为它让我发疯...为了工作,您的 springmvc 文件中必须有一个 ......然后只需第 1 步和2 个是必需的。
【解决方案2】:

仅使用 redirectAttributes.addFlashAttribute(...) -> "redirect:..." 也可以,不必“重新插入”模型属性。

谢谢,aborskiy!

【讨论】:

  • 没错。重新插入是误导。重要的是redirectAttributes.addFlashAttribute。第二个处理程序不必期待额外的 @ModelAttribute 参数。
【解决方案3】:

我认为最优雅的方式是在 Spring MVC 中实现自定义 Flash Scope。

闪存范围的主要思想是将数据从一个控制器存储到第二个控制器中的下一个重定向

请参考我对自定义范围问题的回答:

Spring MVC custom scope bean

此代码中唯一缺少的是以下 xml 配置:

<bean id="flashScopeInterceptor" class="com.vanilla.springMVC.scope.FlashScopeInterceptor" />
<bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
  <property name="interceptors">
    <list><ref bean="flashScopeInterceptor"/></list>
  </property>
</bean>

【讨论】:

    【解决方案4】:

    您可以通过使用 org.springframework.web.servlet.mvc.support.RedirectAttributes 来解决它。

    这是我的控制器示例。

    @RequestMapping(method = RequestMethod.POST)
        public String eligibilityPost(
                @ModelAttribute("form") @Valid EligibiltyForm form,
                Model model,
                RedirectAttributes redirectAttributes) {
            if(eligibilityService.validateEligibility(form)){
                redirectAttributes.addFlashAttribute("form", form);
                return "redirect:<redirect to your page>";
            }
           return "eligibility";
        }
    

    在我的博客上阅读更多内容 http://mayurshah.in/596/how-do-i-redirect-to-page-keeping-model-value

    【讨论】:

      【解决方案5】:

      我有同样的问题。

      刷新页面后使用 RedirectAttributes,我的第一个控制器的模型属性已经丢失。我以为这是一个错误,但后来我找到了解决方案。 在第一个控制器中,我在 ModelMap 中添加属性并执行此操作而不是“重定向”:

      return "forward:/nameOfView";

      这将重定向到您的另一个控制器,并保留第一个控制器的模型属性。

      我希望这就是您想要的。对不起我的英语

      【讨论】:

      • 当我这样做时,模型属性会丢失
      【解决方案6】:

      如果您只想将所有属性传递给重定向...

      public String yourMethod( ...., HttpServletRequest request, RedirectAttributes redirectAttributes) {
          if(shouldIRedirect()) {
              redirectAttributes.addAllAttributes(request.getParameterMap());
              return "redirect:/newPage.html";
          }
      }
      

      【讨论】:

        【解决方案7】:

        也许你可以这样做:

        不要在第一个控制器中使用模型。将数据存储在其他一些共享对象中,然后可由第二个控制器检索。

        查看thisthis 的帖子。这是关于类似的问题。

        附言

        您可能可以使用session scoped bean 来获取共享数据...

        【讨论】:

          【解决方案8】:

          我使用了@ControllerAdvice,请检查Spring 3.X是否可用;我在 Spring 4.0 中使用它。

          @ControllerAdvice 
          public class CommonController extends ControllerBase{
          @Autowired
          MyService myServiceInstance;
          
              @ModelAttribute("userList")
              public List<User> getUsersList()
              {
                 //some code
                 return ...
              }
          }
          

          【讨论】:

            【解决方案9】:

            将所有模型属性作为查询字符串添加到重定向 URL。

            【讨论】:

              【解决方案10】:

              通过使用@ModelAttribute,我们可以将模型从一个控制器传递到另一个控制器

              [第一个控制器的输入][1]

              []:https://i.stack.imgur.com/rZQe5.jpg 从 jsp 页面第一个控制器将带有 @ModelAttribute 的表单数据绑定到用户 Bean

              @Controller
              public class FirstController {
                  @RequestMapping("/fowardModel")
                  public ModelAndView forwardModel(@ModelAttribute("user") User u) {
                      ModelAndView m = new ModelAndView("forward:/catchUser");
                      m.addObject("usr", u);
                      return m;
                  }
              }
              
              @Controller
              public class SecondController {
                  @RequestMapping("/catchUser")
                  public ModelAndView catchModel(@ModelAttribute("user")  User u) {
                      System.out.println(u); //retrive the data passed by the first contoller
                      ModelAndView mv = new ModelAndView("userDetails");
                      return mv;
                  }
              }
              

              【讨论】:

              • 在第二个控制器 Spring 中创建新的用户对象并使用新的用户对象设置第一个控制器用户对象的值。
              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2018-05-25
              • 1970-01-01
              • 2016-07-29
              • 2015-03-02
              • 2019-12-07
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多