javaspringmodel-view-controllermodelandview

Can you convert a Model to a ModelAndView?


I have a spring MVC application and I want to call from a controller a method that requires a ModelAndView as attribute. In my controller method I have the Model. I can't change my controller to use ModelAndView instead of Model and I can't change the signature of the method I want to call.

Can I somehow convert the Model to an equivalent ModelAndView?


Solution

  • If view is not important, than:

    ModelAndView view = new ModelAndView();
    view.addAllObjects(model.asMap());