Search code examples
javamodel-view-controllerdesign-patternsspring-mvc

Controller belongs to the Presentation layer?


I heard that controller belongs to the presentation layer. How is it possible?

I thought that :

  • view is for presentation
  • model is for business logic
  • controller is for controlling logic

Is there good link to prove that controller belongs to the presentation layer?

"Spring MVC is used for presentation layer": how can we use MVC only in the presentation layer?


Solution

  • The presentation layer contains views and controllers.
    You must not mistake an MVC architecture for a multitier/layer architecture (especially a 3-tier architecture). Most of the time Model/View/Controller is not the primary design of a web application, it is just a subset of a multitier/layer architecture.

    Take a look at this oversimplified scheme (you could have the DAOs in a dedicated Data Access Layer, but this is not important in this post) :

    Simplified layers

    Spring MVC is a presentation framework : it deals with controllers and views. But why the "M" in Spring MVC ? Just because, as many other presentation framework, it naturally deals with a representation of a model/entity ("M"). This representation is the one used in your controllers, displayed in your views, submitted in your forms, etc. That's why the framework is called Spring MVC, even if the model/entity is not part of the prensentation layer.

    I think it is a good name for this Framework, because it is really "MVC" oriented. Indeed the representation of a model/entity can be :

    • direct : the framework directly handles the model/entity object
    • indirect : the framework handles a form object or DTO, that contains information related to one or multiple entities

    Spring's recommendation is to directly use the model/entity ("M") object :

    Reusable business code, no need for duplication. Use existing business objects as command or form objects instead of mirroring them to extend a particular framework base class.

    That's why I say the framework is very "MVC" oriented, compared to others, like Struts, where you have to use different form objects.

    Some interesting links :