VOOZH about

URL: https://www.geeksforgeeks.org/advance-java/how-to-create-your-first-view-in-spring-mvc/

⇱ How to Create Your First View in Spring MVC? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Create Your First View in Spring MVC?

Last Updated : 23 Apr, 2026

Spring MVC is 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.

  • A controller method returns a view name, which is resolved to a UI template (JSP/Thymeleaf)
  • View Resolver configuration determines how view names map to actual files

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

  • Create a new Dynamic Web Project in STS/Eclipse
  • Configure project structure properly

Step 2: Adding Dependencies

  • Add spring-webmvc and jakarta.servlet-api
  • Use Maven/Gradle for dependency management

pom.xml:

3. Configure Apache Tomcat Server

  • Add and configure Tomcat server in IDE
  • Deploy the project on the server

4. Configure DispatcherServlet (web.xml)

  • Define DispatcherServlet in web.xml
  • Map it to / to handle all incoming requests

web.xml:

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

  • Create dispatcher-servlet.xml inside WEB-INF
  • Enable component scanning for controllers
  • Configure InternalResourceViewResolver

dispatcher-servlet.xml:

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 <InternalResourceViewResolver> helps Spring find the right JSP file when a view name like "demo" is returned by a controller.

6. Create Controller Class

  • Create a class annotated with @Controller
  • Define request mapping using @RequestMapping or @GetMapping
  • Return view name (e.g., "demo")

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)

  • Create views folder inside WEB-INF
  • Add JSP file (e.g., demo.jsp)
  • Write basic HTML content

demo.jsp:

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:

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
Comment
Article Tags:

Explore