Spring MVC - Multiple View Page

Last Updated : 23 Jul, 2025

A view page is redirected to another view page in this example. Let's look at a simple Spring Web MVC framework sample. The procedure is as follows:

  1. In the case of Maven, load the spring jar files or add dependencies.
  2. Make your controller class.
  3. Provide a controller entry in the web.xml file.
  4. In a separate XML file, define the bean.
  5. Make the rest of the view components.
  6. Start the server and make the project available.

Example Project

Project structure:

Project structure

Step 1. Add dependencies to pom.xml

You can download the required dependencies from URLs given in the comments of the program.

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>

Step 2. Make your request page

Let's start by making a simple jsp page with a link.

index.jsp

HTML
<html>
<body>
    <a href="hello">Click here to go next...</a>
</body>
</html>

Step 3. Develop a controller class

Let's start by developing a controller class that returns JSP pages. To map the class, we supply the exact name using a @Requestmapping annotation.

GfgController.java

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 4. Provide the entry of controller in the web.xml file

web.xml

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 5. In the XML file, define the bean

  • We now provide a view resolver with the view component.
  • For the ViewResolver, the InternalResourceViewResolver class is utilized.
  • For the view component, the prefix+string returned by the controller+suffix page will be used.
  • This XML file should be placed in the WEB-INF folder.

spring-servlet.xml

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 6. Create the other view components

viewpage.jsp

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

final.jsp

HTML
<html>
<body>
    

<p>Welcome to GeeksForGeeks</p>


</body>
</html>

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

Comment