Why is MVC used on the server? Isn't this a client side template?

I am trying to learn web development.

I understand (basically) the concept of MVC, but I'm confused about why the MVC model is used on the server side ... e.g. Spring MVC. Isn't the server side the model and services, and then the client-side services, views, and controllers (AngularJS even makes this template explicit on the client side)?

I am really struggling with how the MVC model fits in or facilitates server-side development.

+4
source share
2 answers

MVC is a template used by far more than just web applications. Any user interface application can use the MVC pattern.

The idea is that you have a view (html or a window in your OS or even a report or something else) and you have a model that represents the dynamic parts of this view. Then you have a controller that is designed to process input and execute “business logic” to create the model and apply it to the view.

So, for example, on the server you might have this MVC pattern:

  • The controller receives the HTTP request and processes it.
  • He creates a model
  • The model is applied to the view to generate HTML and send it in response.

On the client, it will be similar (but slightly different in the case of Angular):

  • A controller is used to define and control the model.
  • Then the model is tied to your view through directives. (Angular is really more like the MVVM pattern, but it is pretty similar)
  • The view is likewise related to your model using directives. (this includes the MVVM part).
  • The idea here is that both the model and the view are updated in accordance with the directives.
  • The controller simply contains the "business logic" for managing the model.

Clean like dirt?

Do not worry. Just know this: this is just an ordinary picture. It is not "server specific" or "client". It can be used anywhere, all that is required to clear data in boilerplate outputs.


EDIT: more thoughts.

In the case of a web API that serves JSON (or even XML) on the server, you still use MVC in most cases. This is because you are doing the following:

  • Process the request in the controller.
  • Create a model in the controller.
  • Relate the model to a "view", which in this case is a view that serializes it as JSON.
+5
source

On good days, the client side was just the display. The server was responsible for communicating with the model, applying business logic, creating a view, and sending static, visualized content back to the client (browser).

As the network matures, some of these responsibilities are transferred from the server to the client. Now the server side is often a thin layer, such as a RESTful API, which stores the “official” business logic (rather than user-friendly logic on the client) and stores the model. But for performance and user experience, the client now saves a copy of the model at its own model level, exchanging with the server and / or local storage as needed and having its own controllers and view logic to provide a stunning user interface.

So, is MVC still applied on the server? Yes! This is completely different. The server often generates the initial view from which the application is launched on the client side (for example, taking into account localization or internationalization) and the official model is still located. But more importantly, the "look" in MVC has just changed. Instead of the server-side view being HTML, it is now the JSON or XML that the client application consumes, not just renders.

So, for the sake of functionality, we are still using MVC on the server. But for an awesome user interface, we also use client side MVC.

+4
source

All Articles