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

Last Updated : 20 Apr, 2026

Spring MVC is a powerful Web MVC framework for building web applications. It is designed around the Model-View-Controller (MVC) pattern, which separates the application into three main components Model, View and Controller.

Prerequisites

Step-by-Step Implementation

Followings are the steps to implement the first model in Spring MVC:

Step 1: Create Dynamic Web Project

Step 2: Add Spring JAR Files

Step 3: Configure Apache Tomcat Server

Step 4: Configure Dispatcher Servlet (web.xml)

**File: web.xml:

XML `

springmvc-view-resolver index.html index.jsp index.htm default.html default.jsp default.htm

<absolute-ordering />

<servlet>
    <!-- Provide a Servlet Name -->
    <servlet-name>viewresolver-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>viewresolver-dispatcher</servlet-name>
    <!-- Provide a url pattern -->
    <url-pattern>/demo.com/*</url-pattern>
</servlet-mapping>

`

**Note: One should be well aware of what is Dispatcher Servlet in Spring as it is a crucial concept to understand prior adhering ahead.

Step 5: Create Spring Configuration 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

viewresolver-dispatcher-servlet.xml

So either you can create a Spring Configuration File or you can just create a simple XML file and add the below lines of code inside that file.

**File: viewresolver-dispatcher-servlet.xml:

XML `

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

`

Step 6: Create Controller Class

@Controller
public class DemoController {}

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

// Annotation
@RequestMapping("/hello")
// 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.

Step 7: Create View (JSP Page)

File: demo.jsp:

HTML `

GeeksforGeeks Welcome Page!

`

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

return "demo";

**File: DemoController.java:

Java `

// Java Program to Illustrate DemoController Class

package com.demo.controllers;

// Importing required classes import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping;

// Class @Controller public class DemoController {

// Method
@RequestMapping("/hello") public String helloWorld()
{
    // Just return the page name
    // No Path, no extension
    return "demo";
}

}

`

Step 8: Create Model and Send Data

Modify controller method:

**File: DemoController.java:

Java `

package com.demo.controllers;

// Importing required classes import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping;

// Class @Controller public class DemoController {

// Method 
@RequestMapping("/hello")
public String helloWorld(Model model) {
  
    // Sending data to view (jsp page)
    String myName = "Amiya Rout";
    model.addAttribute("myNameValue", myName);
    
    // Just return the page name
    // No Path, no extension
    return "demo";
}

}

`

**Step 9: Display Data in JSP

${myNameValue}

Now the complete code for Demo.jsp is given below and you are done.

HTML `

GeeksforGeeks Welcome Page!


My name is: ${myNameValue}

`

**Step 10: Run Application

After that use the following URL to run your controller

http://localhost:8080/springmvc-view-resolver/demo.com/hello

**Output:

Output