Open In App

Spring MVC with JSP View

Last Updated : 01 Aug, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Spring MVC architecture is a part of the Spring Framework that allows building flexible and loosely coupled web applications. One of the most commonly used view technologies with Spring MVC is JSP (JavaServer Pages). This article covers how JSP integrates with Spring MVC

What is Spring MVC?

Spring MVC is a web framework based on the Model-View-Controller design pattern. The terms model, view and controller are defined as follows:

  • Model: The Model encapsulates the application data.
  • View: The View renders the model data and generates HTML output that the client's browser can interpret.
  • Controller: The Controller processes the user requests and passes them to the view for rendering.

Step to implement Spring MVC with JSP

Step 1: Create the Project

  • Eclipse IDE: File -> New -> Dynamic Web Project (check "Generate web.xml")
  • IntelliJ: New -> Project > Maven -> Web App archetype

Step 2: Add Maven Dependencies

Add below dependecies in pom.xml file:

Java
<dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>4.3.7</version>
            </dependency>
             <dependency>
                <groupId>jstl</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
            </dependency> 
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>3.0.1</version>
                <scope>provided</scope>
            </dependency>

Step 3: Configure web.xml

This file tells the servlet container how to load the Spring DispatcherServlet and which file has Spring configurations.

web.xml

Java
<web-app>
  <display-name>SampleMVC</display-name>
  <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener
  </listener-class>
  </listener>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>
  
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

    <servlet-mapping>
       <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
  

      <welcome-file-list>
      <welcome-file>
      /index.html
      </welcome-file>
      </welcome-file-list>

Step 4: Create Spring Configuration file

This file initializes Spring MVC, scans your Java classes, and sets up view resolution (JSP mapping) WebApplicationContext. And this contains the MVC-specific configurations including view-resolvers, datasource, messagesource, multipart-resolver (file-upload), etc.

mvc-dispatcher-servlet.xml

Java
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="...">

  <!-- Scan all Java classes in this package for Spring annotations -->
  <context:component-scan base-package="com.springsamples" />

  <!-- Enable Spring MVC Annotations (@Controller, @GetMapping, etc.) -->
  <mvc:annotation-driven />

  <!-- Allow static content (CSS, JS, images) -->
  <mvc:default-servlet-handler />

  <!-- Configure JSP view resolver -->
  <bean id="viewProvider" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/" />
    <property name="suffix" value=".jsp" />
  </bean>
</beans>

Step 5: Create the Controller

Java
package com.springsamples.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HomeController {

    @GetMapping("/") public ModelAndView firstView()
    {
        // "greet" will map to greet.jsp
        ModelAndView mav = new ModelAndView("greet");
        mav.addObject(
            "greeting",
            "GeeksForGeeks Welcomes you to Spring!");
        return mav;
    }
}

Step 6: Create the JSP View.

Place this file inside: src/main/webapp/greet.jsp

greet.jsp

Java
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" isELIgnored="false" %>
<!DOCTYPE html PUBLIC "-// W3C// DTD HTML 4.01 
 Transitional// EN" "http:// www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; 
 charset=ISO-8859-1">
<title>Start Spring MVC</title>
</head>
<body>
<h1>Start here</h1>
${greeting}
</body>
</html>

When you run the project and open the browser at:

http://localhost:8080/SpringMVCApp/

Output:

output38-300x158
Output

Article Tags :

Similar Reads