Spring MVC Multiple View Page (original) (raw)

Last Updated : 13 Jun, 2026

Spring MVC allows navigation between multiple view pages using controller methods. A controller can return different view names, and the configured View Resolver maps those names to JSP pages. This is commonly used to move users from one page to another based on application flow.

Working Flow

Step-by-Step Implementation of Multiple View Pages in Spring MVC

Follow these steps to create a Spring MVC application to show Multiple View pages.

Step 1: Create a Maven Project

Then Enter the following details:

Click Finish.

Step 2: Add Required Dependencies

Add the following maven dependencies and plugin to your pom.xml file.

XML `

4.0.0 com.geeksforgeeks SpringMVCMultipleViewPage war 0.0.1-SNAPSHOT SpringMVCMultipleViewPage Maven Webapp http://maven.apache.org

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>3.8.1</version>
    <scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.1.1.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>  
    <groupId>javax.servlet</groupId>  
    <artifactId>servlet-api</artifactId>  
    <version>3.0-alpha-1</version>  
</dependency> 
SpringMVCMultipleViewPage

`

Below is the final project structure of the Spring MVC project after creating *.java and *.jsp files also.

Project structure

Step 3: Create Home Page (index.jsp)

This page acts as the entry point of the application and contains a link to navigate to the next view page.

HTML `

Click here to go next...

`

Step 4: Create Controller Class (GfgController.java)

The controller handles incoming requests and returns appropriate view names based on URL mappings.

Java `

package com.geeksforgeeks;

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

@Controller public class GfgController {

@RequestMapping("/hello") public String redirect()
{
    return "viewpage";
}

@RequestMapping("/helloagain") public String display()
{
    return "final";
}

}

`

Step 5: Configure DispatcherServlet (web.xml)

This file registers the Spring MVC DispatcherServlet and forwards incoming requests to Spring for processing.

XML `

SpringMVC spring org.springframework.web.servlet.DispatcherServlet 1 spring /

`

Step 6: Configure Spring MVC (spring-servlet.xml)

This file enables component scanning and configures the View Resolver used to locate JSP pages.

XML `

<!-- Add support for component scanning -->
<context:component-scan base-package="com.geeksforgeeks" />

<!--Add support for conversion, formatting and validation -->
<mvc:annotation-driven/>

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/"></property>
    <property name="suffix" value=".jsp"></property>        
 </bean>

`

Step 7: Create Intermediate View Page (viewpage.jsp)

This page contains another link that redirects the user to the final view page.

HTML `

We are going to visit GeeksForGeeks

`

Step 8: Create Final View Page (final.jsp)

This page is displayed after the user navigates through all configured view pages.

HTML `

Welcome to GeeksForGeeks

`

Step 9: Run the Application

Open the URL:

http://localhost:8080/SpringMVCMultipleViewPage/index.jsp

**Output:

Output

After clicking the "Click here to go next..." link following page will be shown

Output

And after clicking on the above link this page will be shown

Output

**Explanation: In this Spring MVC application, the user starts from the home page and navigates through multiple JSP pages using hyperlinks. The controller receives each request and returns a view name, while the View Resolver maps the returned view names to their corresponding JSP files. This approach helps manage page navigation and user flow in a structured manner.

Advantages

Limitations