You are on page 1of 2

Spring MVC is a component part of the Spring Framework.

Spring MVC is a web framework


based on MVC, it takes advantages of the following principles

• Model, View and Controller (layer separation)

• Dependency Injection (we can inject a component through its interface and not through its
specific type, the idea is to use more abstract types)

• Oriented to the use of Interfaces (Our class that we are using for example of a data access
could change and that implies having to modify all the classes and code where it is being used,
while with interfaces it is much more generic, much more abstract and we only implement a
new class of that interface, but that abstract type remains)

• Use of POJO classes (Simple Java classes with attributes and getter and setter methods
completely decoupled from the framework, do not implement any interface or framework or
API classes and are very reusable, they represent the data of our application. Typically we use it
with JPA when we map to the tables, the Entity classes that are mapped to database tables are
JPA Hibernate classes finally they are POJO simple JAVA classes with attributes and their getter
and setter)

Model Vista Controller

Far beyond a Design Pattern is an Architecture Pattern, as it internally encompasses several


design patterns, an important part of a web framework.

Separate our application into three large components, the controller, the view and the model.

1. The user through a browser interacts directly with the controller, sending requests and
requests to HTTP. Therefore, there is a communication between the user the browser and the
controller. The controller will receive these user requests and will handle them. For example, if
the user sends an ID a piece of information, he will take this ID and look for a record in the
database (that is, the model).

2. The controller interacts with the model, then the model is already in charge of working with
the data, be it queries, operations, select, updates with INSERT, with UPDATE, with DELETE, in
short and this is done through other components classes such as data access, or DAOs or
repositories, services classes that group several DAOs handle transactions, in short, the model
refers to the business logic, the data of our application, this data returns to the controller, the
controller passes it on view.
3. The view is responsible for rendering, for displaying the model data, as can be seen in the
previous figure. Therefore, the view already has access to the model object for example with its
getter and setter attributes and can display this model data.

If we work with Hibernate JPA, the model would be our Entity classes that are also POJOS with
attributes, setter and getter methods. The view can not only display data but can also send data
to the controller, for example, a view that is a form, there we have the reverse process from the
view to the controller.

When we do a submit and an HTTP request of type POST. The model is therefore constantly
interacting and mapping to tables in the Database.

How Spring MVC Works

First there is Front Controller (DispatcherServlet) is a design pattern that is within Spring MVC,
this pattern is responsible for receiving HTTP requests from the client, the user obtains this URL
route, remember that the controllers are mapped to URL routes, obtain this route and will look
for the controller that corresponds to this route and assigns the request to this controller, to
obtain the data the parameters that are being sent. Recall that these routes are mapped with
RequestMapping with GetMapping with PostMapping depending on the verb or type of the
HTTP request, the method.

The controller also interacts or relates to business logic, services or data access classes and for
example obtains this data through a query and sends it to the view, passes it to data in sight to
show this model data in sight. And how does it happen? using the Model object, ModelAndView
the Java Map, the ModelMap in short.

The controller returns, or assigns a logical name of the view to be displayed. The Handler method
returns a String, in that String we indicate the name of the view internally because this is not
seen in the Framework that handles it implicitly, but there is a ViewResolver a handle that
resolves the view, by default the view is HTML if we are working with Thymeleaf, but we can
configure so that the view is of the PDF or Excel type, a flat file or any other type.
Finally, the view is shown to the client using the data of the Model object.
Clear separation of duties within Spring MVC
Controller, validators, form object, DispatcherServelt, handler mapping, view resolve, etc.
They carry out a specific task and can

You might also like