Spring MVC is a Java framework that is used to develop flexible and loosely coupled web applications. It follows the MVC (Model View Controller) pattern. It follows all the basic features of core spring like Dependency Injection, Inversion Control, etc. It can be used in the Spring framework using DispatcherServlet.
DispatcherServlet is a class that maps incoming requests to the right resources.
- Model: It encapsulates the data of the application, which can be a single object or a collection of objects.
- View: It represents the information in a particular format. Thymleaf is used to create views.
- Controller: It contains the business logic of the application.
- Front Controller: DispatcherServlet is used as a front controller.
How Spring MVC works?
The incoming request is first received by the DispatcherServlet. The DispatcherServlet then consults the HandlerMapping and forwards the request to the Controller. The Controller calls the appropriate service methods and returns an object of ModelAndView containing the model data and view name. To find the View, the DispatcherServlet sends the view name to the ViewResolver. The DispatcherServlet now passes the model object to the View. The View then returns the result to the user.
Some related terms related to flow diagram:
The first step in the MVC flow is when a request is received by the Dispatcher Servlet.
Now, the Dispatcher Servlet will with the help of Handler Mapping understand the Controller class name associated with the received request. Once the Dispatcher Servlet knows which Controller will be able to handle the request, it will transfer the request to it.
The Controller will process the request based on appropriate methods and will return it to Model Data and View Name.
Controllers are identified by the @Controller annotation and accompanied by a @RequestMapping annotation. The annotation defines the HTTP verb (standard HTTP commands like GET, POST, PUT, and DELETE) and URI for which the request-mapping method will be applied.
Model and View
It will return the processed data to the Dispatcher Servlet.
Here, POJO class will be annotated with JPA (Java Persistence API) annotations. In this case there are three annotations:
- @Entity identifies an entity that can be persisted to a database.
- @Id identifies the id field as the primary key of the entity.
- @GeneratedValue tells the JPA EntityManager that the key should be automatically generated in the database.
Once Model and View receive the data, Dispatcher Servlet will transfer it to the View Resolver to get the actual view page.
Finally, the Dispatcher Servlet will pass the Model object (results) to the view page. This is the final step of the flow where the results will be displayed.
The CrudRepository interface includes the following methods:
- findById finds the entity in the database with the specified ID.
- findAll returns all entities of the repository type from the database (note that there are other Spring Data repository interfaces, such as PagingAndSortingRepository, that can help manage larger data sets).
- findAllById passed a collection of IDs, this method returns all entities for those IDs.
- save persists an entity to the database (create or update).
- saveAll saves a collection of entities to the database.
- delete deletes the specified entity.
- deleteById deletes the entity with the specified ID.
- deleteAll deletes all entities managed by the repository.
- count returns the number of entities that are in the database.
- existsById returns true if an entity with the specified ID exists in the database.