在春季启动中,用于名称模型视图的ModelAndView引用为空

时间:2018-09-24 11:52:17

标签: spring spring-boot

我有一个名为product的jsp页面。我已经使用ModelAndView从控制器发送一些消息到jsp页面。但它返回null。 ModelAndView的消息未设置为jsp页面。这是我的控制器代码。

@RequestMapping(value = "/add", method = RequestMethod.POST)
  public ModelAndView adduser(@ModelAttribute("productForm") Errors error, Model model) {

    ModelAndView modelAndView = new ModelAndView("product");
    System.out.println(modelAndView);
    modelAndView.addObject("outMessage", "User Registration Success");
    return new ModelAndView("redirect:/productSetup");
  }

请帮助我。

1 个答案:

答案 0 :(得分:2)

重定向时,您应该使用重定向Flash模型属性。

 @RequestMapping(value = "/add", method = RequestMethod.POST)
      public ModelAndView adduser(@ModelAttribute("productForm") Errors error, RedirectAttributes redir) {

        ModelAndView modelAndView = new ModelAndView("product");
        System.out.println(modelAndView);
        redir.addFlashAttribute("outMessage", "User Registration Success");
        return new ModelAndView("redirect:/productSetup");
      }

请参阅此以获取更多信息:https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/mvc/support/RedirectAttributes.html