Nothing Special   »   [go: up one dir, main page]

Open In App

Spring – MVC Framework

Last Updated : 21 Jan, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The Spring MVC Framework follows the Model-View-Controller architectural design pattern which works around the Front Controller i.e. the Dispatcher Servlet. The Dispatcher Servlet handles and dispatches all the incoming HTTP requests to the appropriate controller. 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 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.
Dispatcher Servlet

The Dispatcher Servlet is the front controller that manages the entire HTTP request and response handling process. Now, the question is What is Front Controller? It is quite simple, as the name suggests,

  • When any web requests are made, the requests will come first to the front Controller which is nothing but the Dispatcher Servlet.
  • The Front Controller stands first that is why it’s name is like this. After the requests comes into this, the dispatcher servlet accepts the requests and decides which controller will be suitable to handle these requests.
  • Then it dispatches the HTTP requests to specific controller.
Spring MVC Flow Diagram

Spring-MVC-Framework-Control-flow-Diagram

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.
Steps to Create Spring MVC ApplicationStep 0: Setup the Project

Setup your project with maven use the required archtype to get the required folders directory and configure the server with your project.

Step 1: Add Dependencies in pom.xml

Load the spring jar files or add the dependencies if Maven is used. Add the following dependencies in pom.xml.

pom.xml:

XML
<project xmlns="http://maven.apache.org/POM/4.0.0" \
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
           xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
                             http://maven.apache.org/maven-v4_0_0.xsd">  
  <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>  
  <url>http://maven.apache.org</url>  
  <dependencies>  
    <dependency>  
      <groupId>junit</groupId>  
      <artifactId>junit</artifactId>  
      <version>3.8.1</version>  
      <scope>test</scope>  
    </dependency>  
      
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->  
    <dependency>  
      <groupId>org.springframework</groupId>  
      <artifactId>spring-webmvc</artifactId>  
      <version>5.1.1.RELEASE</version>  
    </dependency>  
  
    <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->  
    <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: Define the Controller

Create a controller class that maps a web request to a response.

HelloGeek.java:

Java
@Controller  
public class HelloGeek {  
@RequestMapping("/")  
    public String display()  
    {  
        return "hello";  
    }     
}  


Step 3: Configure web.xml

Specify the DispatcherServlet in the web.xml file:

web.xml:

XML
<?xml version="1.0" encoding="UTF-8"?>  
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xmlns="http://java.sun.com/xml/ns/javaee" 
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
                             http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">  
  <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: Define the Bean Configuration

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"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/mvc
                           http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="com.example.controller"/>
    <mvc:annotation-driven/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

In this configuration,

  • We have added the necessary XML namespaces and schema locations.
  • <context:component-scan> tells Spring where to look for annotated components.
  • <mvc:annotation-driven> enables Spring MVC annotations.
  • The InternalResourceViewResolver bean is defined to resolve view names. It looks for JSP files in the /WEB-INF/views/ directory and appends .jsp to the view name.
Step 5: Create a JSP File

We will now create a JSP file to display the message.

index.jsp:

HTML
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Spring MVC Example</title>
</head>
<body>
<h2>${message}</h2>
</body>
</html>


This JSP file will display the message passed from the controller.

Step 6: Run the Application

Start the server and run the project. The output is displayed as below:

Spring MVC Tutorial!!


Next Article

Similar Reads

three90RightbarBannerImg