Wednesday, 31 October 2018

Introduction to Spring MVC

MVC stands for Model View Controller. Spring Web MVC is a web framework built on top of Servlet API. it is built on controller pattern and MVC guide lines.

Brief about MVC
In MVC, UI code is never mixed with business logic code.

In MVC, application is divided into 3 components.
         M - Model: Represents the data, a model can be any java object with properties, those can be accessed via getters and setters
         V - View: Represents the user interface (html, jsp)
         C - Controller: Manages the application flow

I would recommend you to go through my below tutorial on mvc pattern.

How Spring MVC works?
I depicted basic flow of spring mvc in below diagram.




1.   In Spring MVC, all the requests are initially redirected to DispatcherServlet. DispatcherServlet is responsible to handle an URI and find the appropriate handlers (i.e. methods declared in Controller classes) and render views (JSPs, html files). DispatcherServlet is also called as FrontController in spring MVC.

2.   Once the DispatcherServlet receives the request, it passes the request to specific handlers using handler mappings.  Handler mappers parse the request url and detect the controller method, that can abel to handle this request. Spring provides many handlers (BeanNameUrlHandlerMapping, RequestMappingHandlerMapping, RequestMappingInfoHandlerMapping, SimpleUrlHandlerMapping, WebSocketHandlerMapping) mapping implementations, you can choose the one that suits to your application needs.
3.   Controller class is responsible for generating the data, specifying the view that can render this data. Controller sends both the data, view information (ModelAndView) details to front controller (DispatcherServlet).
4.   Front controller send the view information to the ViewResoolver to know the actual location of the view page (html, jsp etc.,) that can render the view using this data.
5.   Now the front controller has the java object (that holds the data) and exact location of the view file(jsp, html etc;,). By using the data, view file, front controller send the response back to the user.


Is Spring MVC based on servlet/jsp technologies?
Yes

I am using below maven dependencies throughout the tutorial.

                  <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
                  <dependency>
                           <groupId>org.springframework</groupId>
                           <artifactId>spring-webmvc</artifactId>
                           <version>5.0.6.RELEASE</version>
                  </dependency>

                  <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-validator -->
                  <dependency>
                           <groupId>org.hibernate</groupId>
                           <artifactId>hibernate-validator</artifactId>
                           <version>6.0.10.Final</version>
                  </dependency>

                  <!-- https://mvnrepository.com/artifact/javax.validation/validation-api -->
                  <dependency>
                           <groupId>javax.validation</groupId>
                           <artifactId>validation-api</artifactId>
                           <version>2.0.1.Final</version>
                  </dependency>

Previous                                                 Next                                                 Home

No comments:

Post a Comment