Open In App
Related Articles

How to Create Your First View in Spring MVC?

Like Article
Save Article
Report issue

Spring MVC is a Web MVC Framework for building web applications. The Spring MVC framework is comprised of the following components:

  • Model: A model can be an object or collection of objects which basically 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 logical part of the application. @Controller annotation is used to mark that class as controller.
  • Front Controller: It remains responsible for managing the flow of the web application. DispatcherServlet acts as a front controller in Spring MVC.

As we have discussed the Controller part of how to Create and Run Your First Spring MVC Controller. Here we will be creating our First View in Spring MVC using Spring Tool Suite IDE. 

Note: Views are nothing, they are just web pages. 


Prior to it, certain requirements are needed that are as follows:  

  1. Eclipse (EE version)/STS IDE
  2. Spring JAR Files
  3. Tomcat Apache’s latest version

It is illustrated below step by step as follows:

Note: We are going to use Spring Tool Suite 4 IDE for this project. Please refer to this article to install STS in your local machine How to Download and Install Spring Tool Suite (Spring Tools 4 for Eclipse) IDE? 

Step 1: Create a Dynamic Web Project in your STS IDE. You may refer to this article to create a Dynamic Web Project in STS: How to Create a Dynamic Web Project in Spring Tool Suite?

Step 2: Download the spring JARs file from this link and go to the src > main > webapp > WEB-INF > lib folder and past these JAR files. 

Step 3: Configure Apache Tomcat Server and configure the Tomcat Server with the application. Now we are ready to go.

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. 

Step 4: Go to the src > main > webapp > WEB-INF > web.xml file.

File: web.xml


<?xml version="1.0" encoding="UTF-8"?>
    id="WebApp_ID" version="4.0">
    <absolute-ordering />
        <!-- Provide a Servlet Name -->
        <!-- Provide a fully qualified path to the DispatcherServlet class -->
        <!-- Provide a Servlet Name that you want to map -->
        <!-- Provide a url pattern -->


Step 5: 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.


For example, for this project, the name of the file must be


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.

File: viewresolver-dispatcher-servlet.xml

Creating Spring MVC Controller

Step 6: Now, let’s create some controllers. Go to the src/main/java and create a new controllers package (For ex. com.demo.controllers) as per your choice. And inside that create a Java class and name the class as DemoController. Now how to tell the Spring that this is our controller class. So the way we are going to tell the Spring is by marking it with a @Controller annotation.

public class DemoController {}

Note: Spring will automatically initialize the class having a @Controller annotation and register that class with the spring container.

Now let us create a simple method inside the Controller class and use @RequestMapping annotation before the method something like this.

// Annotation 

// Method 
public String helloWorld() 
    return "";

Now in the return statement, we have to return some views (web pages), so whenever the endpoint ‘/hello’ is invoked we can see our result on the web page. So let’s create our first View.

Creating First View

Go to the src > main > webapp > WEB-INF > right-click > New > Folder and name the folder as views. Then views > right-click > New > JSP File and name your first view. Here we have named it as demo.jsp file. Below is the code for the demo.jsp file. We have created a simple web page inside that file. 

File: demo.jsp


<!DOCTYPE html>
<body bgcolor="green">
    <h1>Hello GeeksforGeeks!</h1>


Now go to the DemoController class and inside the helloWorld() method we have to return a value something like this and we are done.

return "/WEB-INF/views/demo.jsp";

We have just been given the path for our view. So the complete code for the is given below.



package com.demo.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
public class DemoController {
    public String helloWorld() {
        return "/WEB-INF/views/demo.jsp";


Step 7: Add the below line inside the viewresolver-dispatcher-servlet.xml file

<context:component-scan base-package="com.demo.controllers"></context:component-scan>

File: viewresolver-dispatcher-servlet.xml


<?xml version="1.0" encoding="UTF-8"?>
    <context:component-scan base-package="com.student.controllers"></context:component-scan>


Run Spring MVC Application

Step 8: 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





Last Updated : 02 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Similar Reads