Spring – MVC Framework

Spring MVC Framework follows the Model-View-Controller design pattern. It is used to develop web applications. It works around DispatcherServlet. DispatcherServlet handles all the HTTP requests and responses. It dispatches the requests to handlers. It uses @Controller and @RequestMapping as default request handlers. The @Controller annotation defines that a particular class is a controller. @RequestMapping annotation maps web requests to Spring Controller methods. The terms model, view, and controller are as follows:
- Model: The Model encapsulates the application data.
 - View: View renders the model data and also generates HTML output that the client’s browser can interpret.
 - Controller: The Controller processes the user requests and passes them to the view for rendering.
 
Spring MVC Framework works as follows:
- All the incoming requests are intercepted by the DispatcherServlet that works as the front controller.
 - The DispatcherServlet then gets an entry of handler mapping from the XML file and forwards the request to the controller.
 - The object of ModelAndView is returned by the controller.
 - The DispatcherServlet checks the entry of the view resolver in the XML file and invokes the appropriate view component.
 
Advantages of Spring MVC Framework
- The container is used for the development and deployment of applications and uses a lightweight servlet.
 - It enables rapid and parallel development.
 - Development of the application becomes fast.
 - Easy for multiple developers to work together.
 - Easier to Update the application.
 - It is Easier to Debug because we have multiple levels in the application.
 
Disadvantages of Spring MVC Framework
- It has high complexity to develop the applications using this pattern.
 - It is not suitable for small applications which affect the application’s performance and design.
 
Create Your First Spring MVC Application
Consider the following example:
Step 0: Setup your project with maven use the required archtype to get the required folders directory and configure the server with your project.
Step 1: Load the spring jar files or add the dependencies if Maven is used. Add the following dependencies in pom.xml
pom.xml
XML
           xsi:schemaLocation="http://maven.apache.org/POM/4.0.0  <modelVersion>4.0.0</modelVersion>   <groupId>com.javatpoint</groupId>   <artifactId>SpringMVC</artifactId>   <packaging>war</packaging>   <version>0.0.1-SNAPSHOT</version>   <name>SpringMVC Maven Webapp</name>   <dependencies>     <dependency>       <groupId>junit</groupId>       <artifactId>junit</artifactId>       <version>3.8.1</version>       <scope>test</scope>     </dependency>            <dependency>       <groupId>org.springframework</groupId>       <artifactId>spring-webmvc</artifactId>       <version>5.1.1.RELEASE</version>     </dependency>        <dependency>         <groupId>javax.servlet</groupId>         <artifactId>servlet-api</artifactId>         <version>3.0-alpha-1</version>       </dependency>      </dependencies>   <build>     <finalName>SpringMVC</finalName>   </build> </project> | 
Step 2: Create the Controller Class
HelloGeek.java
Java
@Controller public class HelloGeek { @RequestMapping("/")     public String display()     {         return "hello";     }    } | 
Step 3: Provide the name of the controller in the web.xml file as follows:
DispatcherServlet is the front controller in Spring Web MVC. Incoming requests for the HTML file are forwarded to the DispatcherServlet.
web.xml
XML
<?xml version="1.0" encoding="UTF-8"?>          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  <display-name>SpringMVC</display-name>    <servlet>       <servlet-name>spring</servlet-name>       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>       <load-on-startup>1</load-on-startup>     </servlet>   <servlet-mapping>       <servlet-name>spring</servlet-name>       <url-pattern>/</url-pattern>   </servlet-mapping>   </web-app> | 
Step 4: We have to define the bean in a separate XML file. We have specified the view components in this file. It is located in the WEB-INF directory.
spring-servlet.xml
XML
<?xml version="1.0" encoding="UTF-8"?>     xsi:schemaLocation="        <!-- This element defines the base-package where         DispatcherServlet will search the controller class. -->     <context:component-scan base-package="com.geek" />        <!--Provide support for conversion,        formatting and also for validation -->     <mvc:annotation-driven/>    </beans> | 
Step 5: Use JSP to display the message
index.jsp
HTML
<html> <body>    <p>Spring MVC Tutorial!!</p>   </body> </html> | 
Step 6: Start the server and run the project. The output is displayed as follows:
Spring MVC Tutorial!!
				
					


