Flat Preloader Icon

Spring MVC Tutorial

  • Creating a Spring MVC (Model-View-Controller) application involves building a web application that follows the MVC architectural pattern, where the model represents the data, the view handles the user interface, and the controller manages the flow of data between the model and view.
  • Here’s a step-by-step tutorial for building a simple Spring MVC application:

Prerequisites:

  • Java Development Kit (JDK)
  • Integrated Development Environment (IDE) such as Eclipse, IntelliJ IDEA, or Spring Tool Suite (STS)
  • Apache Maven (optional but recommended for dependency management)
  • Tomcat or another Servlet container for deploying the application

Step 1: Set Up a Maven Project

  • Create a new Maven project in your chosen IDE or use the command line.
  • Add the following dependencies to your project’s pom.xml to include Spring MVC and other necessary dependencies:
				
					<dependencies>
    <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.15.RELEASE</version>
    </dependency>
    <dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

				
			

Step 2: Configure Spring MVC

  • Create a Spring configuration file (e.g., dispatcher-servlet.xml) in the src/main/resources directory to configure Spring MVC. Here’s a basic configuration:
				
					<?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.springframe.org/schema/context
http://www.springfram.org/schema/context
/spring-context.xsd
http://www.springf.org/schema/mvc
http://www.springframewor.org/schema/mvc
/spring-mvc.xsd">

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

    <!-- Configure Spring MVC -->
    <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>

				
			

Step 3: Create a Controller

  • Create a controller class (e.g., HelloController.java) to handle incoming requests. In this example, we’ll create a simple “Hello World” controller.
				
					package com.example.controller;

import org.springframework
.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework
.web.bind.annotation
.GetMapping;

@Controller
public class HelloController {

    @GetMapping("/hello")
   public String hello(Model model) 
   {
     model.addAttribute("message",
        "Hello, Spring MVC!");
        return "hello";
    }
}

				
			

Step 4: Create a JSP View

  • Create a JSP view (e.g., hello.jsp) in the src/main/webapp/WEB-INF/views directory to display the message.
				
					<!DOCTYPE html>
<html>
<head>
    <title>Hello Page</title>
</head>
<body>
    <h1>${message}</h1>
</body>
</html>

				
			

Step 5: Configure the Web Deployment Descriptor

  • In the web.xml file located in the WEB-INF directory, configure the Spring DispatcherServlet to handle requests by adding the following servlet and servlet-mapping entries:
				
					<servlet>
<servlet-name>dispatcher
</servlet-name>
<servlet-class>org.springframework.web

.servlet.DispatcherServlet
</servlet-class>
    <load-on-startup>1
    </load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>dispatcher
    </servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

				
			

Step 6: Build and Deploy

  • Build your project using Maven and deploy it to your Servlet container (e.g., Tomcat). Ensure that your container is configured to use the correct Java version.

Step 7: Run the Application

  • Access your application by opening a web browser and navigating to http://localhost:8080/your-web-app-context/hello, where your-web-app-context is the context path of your deployed application.
  • You should see the “Hello, Spring MVC!” message displayed in your browser.
  • Congratulations! You have successfully created a simple Spring MVC web application. You can now expand on this foundation by adding more controllers, views, and functionality as needed for your application.