Skip to main content

Spring MVC Introduction:

  •  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.
  1. MODEL(M)
  2. VIEW(V)
  3. CONTROLLER(C)
  • Spring MVC is one of the Gang of Four design pattern and internally use three design patterns
  1. Strategy
  2. Observer
  3. 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.
  1. Dispatcher Servlet
  2. Handler Mapping
  3. Controller 
  4. View Resolver.
  5. 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:
                1. Create a Maven Project

                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

    Popular posts from this blog

    Java Spring Framework:

    CORE: BASICS:           The new technology I had learned is the spring Framework core which is the base of the Spring technology or framework in Java. The main feature of the spring is that it is light weight where the run time is very less. Then the next advantage is the framework use the dependency injection in the object reference in Java where java is a OOPs programming language. The spring framework was found by rod johnson in the year of 2002 and licensed in the apache company in 2003. The other advantages of the Spring is that it can be used to build every application or software which is currently in the market. The framework supports: AOP Spring Boot Spring JDBC Spring MVC Spring EJB Spring Hibernate.           The diagram from the official website of the Spring is a good representation of the Spring Framework architecture which relies on the Spring Core. Fig 1: Spring Framework       ...

    Properties and Primitive Insertion in Spring:

     Property Files and Values: The Properties is a subclass of Hashtable class and it represents a persistent set of properties. The Properties can be saved to a stream or loaded from a stream. Each key and its corresponding value in the property list is a string. .properties is a file extension for files mainly used in Java-related technologies to store the configurable parameters of an application The Properties file can be used in Java to externalize the configuration and to store the key-value pairs. The Properties.load() method of Properties class is convenient to load .properties file in the form of key-value pairs. In normal java syntax we will use the util.*. to load the properties, URL,  etc. Then we use the load method to load any file and we can use that to retrieve properties. But in spring we can use only one annotation to load the whole properties file and the Annotation is @PropertySource:(file path). The main important job of the property file is to reduce the har...

    Annotations and Scope in Spring:

     Basic Annotations: Component: It is used to create the bean without the configuration files like xml and java configurations The auto wired is used along with it to do the dependency injection Autowired: The Autowired annotation is used for the dependency injection the main types are the: Autowired in constructor Autowired in setter Autowired in the field Note : Using the field injection is not recommended. Fig 1. Autowired in constructor. Fig 2. Autowired in the field. Import: It is used to import other configuration files into the class file.  This will be helpful in the scenario of multiple configuration files are there. Fig 3. Import method usage Primary: The @Primary annotation can be present at the starting of a class where it represent the primary class. For example if we implement two classes using a interface and we gave two classes the @Component annotation the spring will be confused.  So when we add the primary tag on any one of them then that one will be cho...