How does Spring MVC relate to service, controller, and data access levels in a web application?

I understand MVC Pattern , and also how Spring MVC implements it.

However, how Rest controller , Data Access Layer and Service Layer fit into this template?

It:

  • Model = Database (e.g. Oracle / MySQL) and Repositories classes

  • Controller = Service (buisness logic) and Rest Controller classes

  • View = JSP / FreeMarker ?

+5
source share
3 answers

A model is not a database, it is not a repository, not an entity. A model is an abstraction containing all the data that needs to be displayed. And each View has its own model. You can think of Model as a container for data between Controller and View .

The Spring model uses the ModelMap parameter of the controller method.

Controller - prepares the Model to pass its View . If the model is fairly simple, the Controller can do this on its own.

But most models contain a lot of data. It can be several objects from the database, data from the configuration, etc. In this case, the controller uses the lower level: Service , Repository . All of them help ontroller build a model for the View .

upd: The purpose of the Controller is to connect View and Model . Controller creates and populates the Model , then selects the View and passes that created Model to the View . The way Model and View get connected.

Spring controllers have Controller and RestController .

View - the end point at which the data from the Model (transmitted from the Controller ) will be displayed to the user. But another View role is to receive commands from the user and pass it to the Controller .

In Spring, this can be a view of any viewer: JSP , Freemaker , Thymeleaf .


Note: Normally, the Controller does not use the Repository directly. Controller traditionally works with Service , and Service uses Repository to retrieve data from the database. So, the relationships are as follows: View <- Controller β†’ Service β†’ Repository

+7
source

The controller accepts HTTP requests and often downloads or stores some data (from a service or DAO) and returns an HTTP response. This answer may be a redirect or presentation, or some JSON or binary.

The controller can use services, but must avoid its own logic. It can also directly use data access objects if no maintenance logic is required.

A model is that the information that needs to be seen must do its job. This is not necessarily related to the database. For example, you might have a model in a registration form with email and confirmEmailAddress fields. You do not save the confirmEmailAddress field in your db, so there is no 1 to 1 relationship between tables and db models. In addition, your model may be data for a simple calculation that is not saved.

+4
source

In my opinion, it should be:

Model = Service (buisness logic), Repositories, entity classes

Controller = Restore Controller Classes

View = JSP / FreeMarker

-1
source

Source: https://habr.com/ru/post/1243902/


All Articles