Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between return ModelAndView and return String in Spring MVC?

Tags:

spring

I want to know the different between ModelAndView and String.

@RequestMapping(value="/")
    public ModelAndView mainPage() {
        return new ModelAndView("home");
}

and the second piece of code is about returning String:

@RequestMapping(value="/")
    public String mainPage() {
        return "home";
}
like image 684
v11 Avatar asked Sep 23 '14 06:09

v11


People also ask

What is ModelAndView in Spring MVC?

ModelAndView is a holder for both Model and View in the web MVC framework. These two classes are distinct; ModelAndView merely holds both to make it possible for a controller to return both model and view in a single return value. The view is resolved by a ViewResolver object; the model is data stored in a Map .

What is the difference between ModelMap and ModelAndView in spring?

Model is an interface while ModelMap is a class. ModelAndView is just a container for both a ModelMap and a view object. It allows a controller to return both as a single value.

When should I use ModelAndView and model?

Model defines a holder for model attributes and is primarily designed for adding attributes to the model. ModelMap is an extension of Model with the ability to store attributes in a map and chain method calls. ModelAndView is a holder for a model and a view; it allows to return both model and view in one return value.

What happens when the Dispatcherservlet tries to render a view for a given ModelAndView?

the modelandview object contains the model data and the view name. the dispatcherservlet sends the view name to a viewresolver to find the actual view to invoke. now, the dispatcherservlet will pass the model object to the view to render the result.


1 Answers

You can return many things from a controller, and Spring will automatically try to deduce what to do from there.

When you return a ModelAndView all information that needs to be passed from the controller is embedded inside it. No suprise there.

If you return a String, it will assume that this is the name of the view to use. It will wrap this in a ModelAndView object with the string as the view and the existing model embedded as well.

Spring does a lot of 'magic' on the return types, but also on the parameter types. This allows you to write code in a style that is more intuitive for you, i.e. the following two examples are the same:

@RequestMapping(value="/")
public ModelAndView mainPage() {
    Model m = new Model();
    m.put("key", "value");
    return new ModelAndView(m,"main");
}

and

@RequestMapping(value="/")
public String mainPage(Model m) {
    m.put("key", "value");
    return "main";
}

This second variant is a little less verbose and easier to test separately. The model passed in will be an empty model (unless forwarded from some other controller).

like image 84
Niels Bech Nielsen Avatar answered Oct 25 '22 18:10

Niels Bech Nielsen