Spring forward tutorial

last modified October 18, 2023

Spring forward tutorial shows how to forward a request in a Spring web application.

Spring is a popular Java application framework for creating enterprise applications.


The special forward: prefix in a view name performs a forward to different URL. Forwarding a URL transfers the request internally within the same server without involving the client browser. Forwards are performed less often than redirects.

Redirect vs Forward

A request can be basically processed in three ways: a) resolved by Spring in a controller action, b) forwarded to a different controller action, c) redirected to client to fetch another URL.



Spring Forward example

The following application uses forwards to a different URL after a form submission. It performs a forward with the forward: prefix.

│   ├───java
│   │   └───com
│   │       └───zetcode
│   │           ├───config
│   │           │       MyWebInitializer.java
│   │           │       WebConfig.java
│   │           └───controller
│   │                   MyController.java
│   └───resources
│       │   logback.xml
│       │
│       └───templates
│               show.ftl

This is the project structure.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"














In the pom.xml we have the necessary dependencies.

<?xml version="1.0" encoding="UTF-8"?>
    <logger name="org.springframework" level="ERROR"/>
    <logger name="com.zetcode" level="INFO"/>

    <appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
            <Pattern>%d{HH:mm:ss.SSS} %blue(%-5level) %magenta(%logger{36}) - %msg %n

        <level value="INFO" />
        <appender-ref ref="consoleAppender" />

The logback.xml is a configuration file for the Logback logging library.

package com.zetcode.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class MyWebInitializer extends
        AbstractAnnotationConfigDispatcherServletInitializer {

    protected Class<?>[] getRootConfigClasses() {
        return null;

    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{WebConfig.class};

    protected String[] getServletMappings() {
        return new String[]{"/"};

MyWebInitializer registers the Spring DispatcherServlet, which is a front controller for a Spring web application.

protected Class<?>[] getServletConfigClasses() {
    return new Class[]{WebConfig.class};

The getServletConfigClasses returns a web configuration class.

package com.zetcode.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;

@ComponentScan(basePackages = {"com.zetcode"})
public class WebConfig implements WebMvcConfigurer {

    public FreeMarkerViewResolver freemarkerViewResolver() {

        var resolver = new FreeMarkerViewResolver();
        return resolver;

    public FreeMarkerConfigurer freemarkerConfig() {

        var freeMarkerConfigurer = new FreeMarkerConfigurer();
        return freeMarkerConfigurer;

WebConfig configures Freemarker. We set the template files location to templates directory on the classpath. (The resources is on the classpath.)

package com.zetcode.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

public class MyController {

    private static final Logger logger = LoggerFactory.getLogger(MyController.class);

    @GetMapping(value = "/sendname")
    public String send(@RequestParam(defaultValue = "guest") String name, Model model) {

        logger.info("sendname called");

        model.addAttribute("name", name);

        return "forward:/newpage";

    @GetMapping(value = "/newpage")
    public String newpage() {

        logger.info("newpage called");

        return "show";

MyController provides two GET mappings.

@GetMapping(value = "/sendname")
public String send(@RequestParam(defaultValue = "guest") String name, Model model) {

    logger.info("send name called");

    model.addAttribute("name", name);

    return "forward:/newpage";

A request sent to /sendname is processed by send action. A request parameter is read and added to the model. In the end, it is forwarded to a new controller action.

@GetMapping(value = "/newpage")
public String newpage() {

    logger.info("newpage called");

    return "show";

The newpage action resolves the request to a show view.

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

User name: ${name}


The show.ftl display the user name.

$ mvn jetty:run

We run the server and locate to localhost:8080/sendname?name=Peter.

09:52:11.636 INFO  com.zetcode.controller.MyController - sendname called 
09:52:11.653 INFO  com.zetcode.controller.MyController - newpage called 

The server log contains these lines.

In this article we have performed a forward in a Spring controller.


My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.

List all Spring tutorials.