Enamul Haque
Enamul Haque

Reputation: 843

ModelAndView reference to view with name model is null in spring boot

I have a jsp page named product. I have send some message to jsp page from controller using ModelAndView. But it returns null. ModelAndView's Message is not set to jsp page. here is my controller code..

@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");
  }

Please help me..

Upvotes: 2

Views: 4560

Answers (1)

Alien
Alien

Reputation: 15878

You should use Redirect Flash Model attributes when redirecting.

 @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");
      }

Refer this for more info : https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/mvc/support/RedirectAttributes.html

Upvotes: 3

Related Questions