English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Spring MVC multi-view page example

Here, we will redirect one view page to another view page.

Let's look at a simple example of the Spring Web MVC framework. The steps are as follows:

Load the spring jar file or add dependencies in Maven Create a controller class Provide controller entries in the web.xml file Define beans in a separate XML file Create other view components Start the server and deploy the project


1Add dependencies to pom.xml

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

2Create a request page

We create a simple JSP page containing links.

index.jsp

<html>
<body>
<a href="hello">Click here...</a>/a>
</body>
</html>

3Create a controller class

Let us create a controller class that returns a JSP page. Here, we map this class using a specific name with the @RequestMapping annotation.

HelloController.java

package com.w3codebox;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
@RequestMapping("/hello")
    public String redirect()
    {
        return "viewpage";
    }   
@RequestMapping("/helloagain")
public String display()
{
    return "final";
}
}

4Define the controller entry in the web.xml file

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <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>

5Define bean in XML file

Now, we also provide view components for the view resolver.

Here, the InternalResourceViewResolver class is used for ViewResolver.

Controller+prefix of the suffix page returned+The string will be called by the view component.

This xml file should be located in the WEB-INF directory.

spring- servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://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">
    <!-- Provide support for component scanning -->
    <context:component-scan base-package="com.w3codebox" />
    <!--Provide support for conversion, formatting and validation -->
    <mvc:annotation-driven/>
<!-- Define Spring MVC view resolver -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/></property>
        <property name="suffix" value=".jsp"></property>/property>        
     </bean>
</beans>

6、Create Other View Components

viewpage.jsp


<html>
<body>
<a href="helloagain">w3codebox Tutorials/a>
</body>
</html>

final.jsp


<html>
<body>
<p>Welcome to Spring MVC Tutorial</p>/p>
</body>
</html>

Output: