Spring MVC - Multiple View Page

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.

  • Enables navigation between multiple JSP pages.
  • Uses controller methods to return different views.
  • Simplifies page flow management in web applications.

Working Flow

  • User opens the home page.
  • Clicks a link to navigate to another page.
  • Controller receives the request and returns a view name.
  • View Resolver maps the view name to the corresponding JSP page.
  • User is redirected through multiple view pages.

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

  • Open STS IDE.
  • Click File - New - Maven Project.
  • Select Create a simple project (Select archetype ) and click Next.

Then Enter the following details:

  • Group Id: com.gfg
  • Artifact Id: SpringMVCMultipleViewPage
  • Packaging: war

Click Finish.

Step 2: Add Required Dependencies

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

XML
<project xmlns="https://maven.apache.org/POM/4.0.0" 
         xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 
                             https://maven.apache.org/xsd/maven-4.0.0.xsd">
  
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.geeksforgeeks</groupId>
  <artifactId>SpringMVCMultipleViewPage</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>SpringMVCMultipleViewPage Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    
    <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> 
    
  </dependencies>
  <build>
    <finalName>SpringMVCMultipleViewPage</finalName>
  </build>
</project>

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
<html>
<body>
    <a href="hello">Click here to go next...</a>
</body>
</html>

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
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
  <display-name>SpringMVC</display-name>
   <servlet>  
    <servlet-name>spring</servlet-name>  
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    <load-on-startup>1</load-on-startup>    
</servlet>  
<servlet-mapping>  
    <servlet-name>spring</servlet-name>  
    <url-pattern>/</url-pattern>  
</servlet-mapping>  
</web-app>

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

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

XML
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans/"
    xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context/"
    xmlns:mvc="http://www.springframework.org/schema/mvc/"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans/
        http://www.springframework.org/schema/beans//spring-beans.xsd
        http://www.springframework.org/schema/context/
        http://www.springframework.org/schema/context//spring-context.xsd
        http://www.springframework.org/schema/mvc/
        http://www.springframework.org/schema/mvc//spring-mvc.xsd">

    <!-- 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>
  
</beans>

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

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

HTML
<html>
<body>
    <a href="helloagain">We are going to visit GeeksForGeeks</a>
</body>
</html>

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

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

HTML
<html>
<body>
    

<p>Welcome to GeeksForGeeks</p>


</body>
</html>

Step 9: Run the Application

  • Right-click the project.
  • Select Run As - Run on Server.
  • Choose Apache Tomcat Server.
  • Click Finish.

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

  • Simplifies navigation between multiple pages.
  • Centralizes page flow logic inside controllers.
  • Easy to maintain and extend with additional views.

Limitations

  • Managing many view pages can increase controller complexity.
  • Navigation logic becomes harder to maintain in large applications.
  • Requires proper View Resolver configuration.
Comment