Spring MVC Redirect

In this article, we will learn to redirect request from one action to another by using the prefix redirect: inside the controller class. It depends on the application flow and scenario where to redirect the request. Here, we will redirect a form request to another action after saving the record. We can use it like: redirect:action_name.

It is part of our controller, so will perform this into our controller class. Let's understand by an example and create a maven-based spring project and add these files in it.

We are providing source code files, so you can use them into your project to understand the concept. See the following source code files.

// UserController.java

This is our controller class that contains three methods index(), save() and userSaved(). Here, the first method returns a user form, and the second method save and redirect to the third method and third method simple returns a response view page.

package com.studytonight.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

public class UserController {
	public String index() {
		return "index";
	public String save( ) {
        // data saving stuffs
		return "redirect:user_saved"; // it redirects to other action
	public String userSaved() {
		return "response";

// AppConfig.java

This is a configuration file which is an alternate of the applicationContext.xml file that we created for the XML-based configuration example. The @Configuration annotation indicates that this is not a simple class but a configuration class and the @ComponentScan annotation is used to indicate the location of the component class in our spring project.

package com.studytonight;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

public class AppConfig implements WebMvcConfigurer{
	public ViewResolver viewResolver() {
		InternalResourceViewResolver irvr = new InternalResourceViewResolver();
		return irvr;

// MainApp.java

This file contains code to create an IOC container for our application. The AnnotationConfigWebApplicationContext class is used to create an object for application context.

package com.studytonight;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

public class MainApp implements WebApplicationInitializer {

	public void onStartup(ServletContext servletContext) throws ServletException { 
		AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
		ServletRegistration.Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(context));


View Pages

// index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
<!DOCTYPE html>
<meta charset="UTF-8">
<title>User Form</title>
	<form action="save" method="post">
		User Name: <input type="text" name="user_name"><br><br>
		 Email: <input type="email" name="email"><br> <br>
		 <input type="submit" value="Register">

// response.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
<!DOCTYPE html>
<meta charset="UTF-8">
<title>Response User Data</title>
    <h2>User is added.</h2>

// pom.xml

This file contains all the dependencies of this project such as spring jars, servlet jars, etc. Put these dependencies into your project to run the application.

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
		<!-- https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api -->

Run the Application

After successfully completing the project and adding the dependencies run the application and you will get the output as below.