Spring MVC is a widely used framework that helps build structured web applications using the Model-View-Controller pattern. It simplifies request handling and application flow using plain Java classes. In this guide, you will learn how to create and run your first Spring MVC controller in Eclipse/Spring Tool Suite.
- Uses DispatcherServlet to manage and route incoming requests
- Supports view technologies like JSP or Thymeleaf for UI rendering
- Allows easy data sharing between controller and view using Model objects
Key Concepts in Spring MVC
The Spring MVC framework is comprised of the following components.
- Model: A model can be an object or collection of objects that contains the data of the application.
- View: A view is used for displaying the information to the user in a specific format. Spring supports various technologies like Freemarker, Velocity, and Thymeleaf.
- Controller: It contains the business logic of the application. @Controller annotation is used to mark a class as a controller.
- DispatcherServlet: The front controller in the Spring MVC architecture. It intercepts all incoming requests and delegates them to appropriate controllers for processing.
Here, we are going to create our first spring MVC controller in Spring Tool Suite IDE.
Prerequisites
- Eclipse (EE version)/STS IDE
- Spring JAR Files
- Apache Tomcat latest version
Note: We are going to use Spring Tool Suite 4 IDE for this project. Please refer to this article to install STS on your local machine How to Download and Install Spring Tool Suite (Spring Tools 4 for Eclipse) IDE?
Steps to Build the Application
Follow the below steps to run your first SpringMVC Controller.
Step 1: Create a Dynamic Web Project in your STS IDE
Open your STS IDE, you may refer to this article How to Create a Dynamic Web Project in Spring Tool Suite?
Step 2: Add Spring JAR Files
Download the spring JARs file and go to the src > main > webapp > WEB-INF > lib folder and past these JAR files.
Step 3: Configure Apache Tomcat Server
Configure Apache Tomcat Server with the application. Now we are ready to go.
Step 4: Configuring Dispatcher Servlet
Please refer to this article What is Dispatcher Servlet in Spring? and read more about Dispatcher Servlet which is a very very important concept to understand.
Now we are going to configure Dispatcher Servlet with our Spring MVC application. Go to the src > main > webapp > WEB-INF > web.xml file
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee/index.html"
xsi:schemaLocation="http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee/index.html
http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
<display-name>myfirst-mvc-project</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.jsp</welcome-file>
<welcome-file>default.htm</welcome-file>
</welcome-file-list>
<absolute-ordering/>
<servlet>
<!-- Provide a Servlet Name -->
<servlet-name>frontcontroller-dispatcher</servlet-name>
<!-- Provide a fully qualified path to the DispatcherServlet class -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<!-- Provide a Servlet Name that you want to map -->
<servlet-name>frontcontroller-dispatcher</servlet-name>
<!-- Provide a url pattern -->
<url-pattern>/student.com/*</url-pattern>
</servlet-mapping>
</web-app>
Step 5: Create Spring Configuration File
Now go to the src -> main -> webapp -> WEB-INF and create an XML file. Actually, this is a Spring Configuration file like beans.xml file. And the name of the file must be in this format
YourServletName-servlet.xml
For example, for this project, the name of the file must be
frontcontroller-dispatcher-servlet.xml
So either you can create a Spring Configuration File or you can just create a simple XML file add the below lines of code inside that file.
frontcontroller-dispatcher-servlet.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans/"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context/"
xsi:schemaLocation="http://www.springframework.org/schema/beans/
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context/
https://www.springframework.org/schema/context/spring-context.xsd">
</beans>
Step 6: Creating Spring MVC Controller
Go to src/main/java, create a package (e.g., com.student.controllers), and add a class named DemoController. Mark the class with @Controller to let Spring recognize it as a controller. Use @RequestMapping("/hello") to map a URL endpoint to the controller method.
DemoController.java:
// Java Program to Illustrate DemoController Class
package com.student.controllers;
// Importing required classes
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
// Annotation
@Controller
// Class
public class DemoController {
// Annotation
@ResponseBody
@RequestMapping("/hello")
// Method
public String helloWorld()
{
return "Hello World!";
}
}
Note: Spring will automatically initialize the class having a @Controller annotation and register that class with the spring container.
Step 7: Enable Component Scanning
Add the below line inside the frontcontroller-dispatcher-servlet.xml file
<context:component-scan base-package="com.student.controllers"></context:component-scan>
frontcontroller-dispatcher-servlet.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans/"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context/"
xsi:schemaLocation="http://www.springframework.org/schema/beans/
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context/
https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.student.controllers"></context:component-scan>
</beans>
Step 8: Run the Application
To run your Spring MVC Application right-click on your project > Run As > Run on Server and run your application as shown in the below image.
After that use the following URL to run your controller as shown in the below image. All other details can be perceived through below image as follows:
http://localhost:8080/myfirst-mvc-project/student.com/hello
