How to Create Your First View in Spring MVC? (original) (raw)

Last Updated : 23 Apr, 2026

**Spring MVCis a Web MVC Framework for building web applications. Creating your first view in Spring MVC involves setting up a controller to handle requests and returning a view name that maps to a template (like JSP or Thymeleaf). The framework uses a View Resolver to connect the controller’s response with the actual UI page.

Prerequisites

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

Step-by-Step Implementation

Follow these steps to create and render your first view in Spring MVC by configuring a controller, view resolver, and template page.

**Step 1: Create a Dynamic Web Project

**Step 2: Adding Dependencies

**pom.xml:

XML `

org.springframework spring-webmvc 5.3.23 jakarta.servlet jakarta.servlet-api 5.0.0 provided

`

**3. Configure Apache Tomcat Server

**4. Configure DispatcherServlet (web.xml)

web.xml:

XML `

<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>

`

**Explanation: The DispatcherServlet is the entry point of the application. We can think of a DispatcherServlet as the gatekepper, it handles all the incoming requests. When a request comes, it passes to the DispatcherServlet and then it decides which controller should handle it.

5. Create Spring Configuration File

dispatcher-servlet.xml:

XML `

<!-- Scans for @Controller annotations -->
<context:component-scan base-package="com.demo.controllers" />

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

`

**Explanation: The context:component-scan tells the spring to look through the com.demo.controllers package for any classes marked with @Controller. The classes will handle the requests. The helps Spring find the right JSP file when a view name like "demo" is returned by a controller.

6. Create Controller Class

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

// Annotation

@GetMapping("/hello")

// Method

public String helloWorld()

{

return "demo";

}

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.

7. Create View (JSP Page)

**demo.jsp:

HTML `

Hello GeeksforGeeks!

`

**Explanation: Here, we have created a jsp file, which contains a basic html code and outputs "Hello GeeksforGeeks" in the body with the green background.

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 "demo";

**DemoController.java:

Java `

package com.demo.controllers;

import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping;

@Controller public class DemoController {

@RequestMapping("/hello")
public String helloWorld() {
    return "demo";
}

}

`

Step 8: Run Spring MVC 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.

dispatcherServlet

After that use the following URL to run your controller:

http://localhost:8080/springmvc-view-resolver/hello

**Output:

Output