2

Studying some in-depth concept of Spring framework, I'm facing this doubt.

Has the Model, as inteded in Spring MVC, the same meaning of the M in MVC?

I mean: I always considered the Model of the MVC pattern a layer where is residing all the data and the logic that is retrieving it from a DB.

But in Spring MVC model is kind a place where all the objects shared between Controller and View are living...

Is this a bad interpretation? Am I missing something?

  • 2
    MVC has nothing to do with databases specifically. However, the Model often has persistence of some kind. The deserialization of a Model from a persistence layer (perhaps a database) is not part of the Model in MVC. – Frank Hileman Mar 21 '17 at 15:06
  • 3
    Possible duplicate of Where is the M in MVC? – Greg Burghardt Mar 21 '17 at 17:36
  • 2
    You could replace "Spring MVC" with "ASP.NET MVC" or "Ruby on Rails" and have the same question. This really is applicable to the design pattern, not a specific framework. – Greg Burghardt Mar 21 '17 at 17:37
  • @GregBurghardt I think this question arose due to the diagrams used to illustrate Spring MVC, whereby the Model appears to be a communications bundle. – Frank Hileman Mar 22 '17 at 1:28
  • @FrankHileman yes, this is what I mean... – davioooh Mar 23 '17 at 8:56
3

Is the Model in Spring MVC the same of MVC pattern?

If you mean org.springframework.ui.Model, then no, it isn't.

In MVC, the Model is where your business lives. The Model in MVC manages the data, and domain rules of the application.

In Spring, the Model you are referring to is a holder for data that the view displays. It's basically a glorified Map underneath.

A view is responsible for rendering the model. The way views access the model data isn't the same across all views supported by Spring (JSP, FreeMarker, Portlets, etc) so you need a generic way to expose that data to any view, to abstract away the view technology.

Before Spring 2.5.1 you sent this data as a Map (Spring has a org.springframework.ui.ModelMap with various helper methods on it... but still a Map) then the org.springframework.ui.Model interface was added which is a more semantic alternative to the same thing (it has a org.springframework.ui.ExtendedModelMap implementation which at the same time is a child of ModelMap).

From the ModelMap documentation (emphasis mine):

Implementation of {@link java.util.Map} for use when building model data for use with UI tools. Supports chained calls and generation of model attribute names.

This class serves as generic model holder for both Servlet and Portlet MVC, but is not tied to either of those. Check out the {@link Model} interface for a Java-5-based interface variant that serves the same purpose.

So, this Spring Model interface is just a container of key-value attributes for the View to render. It's called Model most likely because this is a MVC framework with Controllers and Views and Models (i.e. MVC stands for Model View Controller, not Map View Controller - so people relate better to that naming).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.