- It is one of the frame work in the spring where it is used to develop web applications and distributed application.
- As there is servlets and jsp is in place to develop the web applications. But what is the need of spring MVC.
- But the problem in the normal servlet and jsp we need to write lots of boiler plate code. So the Spring MVC framework will reduce totally the boiler plate code.
- The Boiler plate code is the code which is common for all the classes is called the boiler plate code.
- In the project if there is 100 servlets then there should be 100 java beans will be prepared by the developer.
- This will cause the mixing of business logic and presentation logic which means we write the both the logics in same file so that it is difficult to modify the source code if the developer wants.
Spring MVC Advantages:
- Spring MVC will remove the boiler plate code using the Front controller(Dispatcher Servlet).
- It will separate the Business logic and presentation logic.
- MODEL(M)
- VIEW(V)
- CONTROLLER(C)
- Spring MVC is one of the Gang of Four design pattern and internally use three design patterns
- Strategy
- Observer
- Composite
- This composition is the total Spring MVC Framework
The path of the Request in MVC:
- The request will first go to the Dispatcher Servlet from the client.
- In the next step it will go to the Handler Mapping from the Dispatcher Servlet.
- The request not only go to the Handler mapping it also go to the controller, View Resolver also. Both the things will give output to the Dispatch Servlet.
- The Dispatcher Servlet will send the view content to the view part.
- Therefore there are 5 components in Spring MVC architecture.
- Dispatcher Servlet
- Handler Mapping
- Controller
- View Resolver.
- View
- Dispatcher Servlet: It is used to take care of performing some common processing logic that should be applied to all the request that are coming form the client. It will bind the URL pattern like *.htm, *.web, *.mvc, etc...
- Handler Mapping: It is used to identify which controller is to be used to that specific request. Based on the URL the Handler Mapping will return the controller name and then the Dispatcher will execute that controller.
- Controller: Controller is responsible for processing the logic to handle request. The controller always will give logical name as response to the Dispatcher.
- View Resolver: It has the responsibility is to converting the logical name into physical name from the Dispatcher.
- Example : Hello (logical) ------------ Hello.jsp (physical).
- View: When the physical name is called then the view will be displayed. The view is just how to render/display the data.
- Step 1: Every time when a request comes from a client then the dispatcher servlet object is created.
- Step 2:The dispatcher servlet always looks for servlet name, servlet.xml file (dispatcher-servlet.xml).
- So we need to write dispatcher-servlet.xml which will be having the configurations of Handler Mapping, Controller, View resolver. (This is for the XML configuration.)
Creating Spring Containers for Web Applications:
- In standalone applications we can use the Classpath or Annotation but in Web Application we will use the XMLWebApplicationContext();
- Step 3:Spring Container will be created by the dispatcher servlet.
- But the Dispatcher Servlet object is created by the Servlet Container.
- In Spring Container which is creates by the dispatcher servlet we will have Handler Mapping, Controller, View Resolver.
Handler Mapping:
- Step 4: Dispatcher will send request to the Handler Mapping It will hold a table with two columns with controller uri, controller name.
- Step 5: After the Handler mapping return the controller name as the return type to the Dispatcher using the Controller uri.
- Then the Dispatcher will give the control to that corresponding controller.
- Step 6: Then the controller will give the logical name to the Dispatcher from the Controller.
- Step 7: Then that logical name is given to the internal view resolver by the Dispatcher. (InternalViewResolver is the implementation of view resolver interface)
- The IRVR will have two attributes prefix, suffix (eg. prefix="/WEB-INF/jsp", suffix=".jsp").
- Therefore the physical name = prefix + controller logical name+ suffix.
- Step 8: The internal view resolver will send the physical name to the Dispatcher.
The MAV:
- The ModelAndView Class is a class which we will use in the controller of the MVC.
- The Steps for a simple MVC applications are:
2. Then configure the web.xml file for the dispatcher servlet.
3. Then write the controller class
4. Then configure the dispatcher-servlet.xml file with handler mapping, view resolver,
controller.
5. Write jsp file in the WEB-INF/jsp folder.
6. Build the project as war module.
Comments
Post a Comment