Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6980451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:01:12+00:00 2026-05-27T18:01:12+00:00

Is there a way to forward a request to another Controller while adding some

  • 0

Is there a way to forward a request to another Controller while adding some parameter data to it? I tried adding to the ModelMap, but it doesn’t seem to hang around. I am doing something like:

return "forward:/my-other-controller";

Only other way I can think of is to put the parameters on the session and then pop them off in the target controller.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:01:13+00:00Added an answer on May 27, 2026 at 6:01 pm

    The simplest way is to add the data to the request. Since this is a forward, the same request is passed around to different handlers within the server.

    As example, let’s start with a simple setup of two controllers, one forwarding to the other:

    @Controller
    public class TestController {
        @RequestMapping(value="/test")
        public String showTestPage() {
            return "forward:/test2";
        }
    }
    
    @Controller
    public class TestController2 {
        @RequestMapping(value="/test2")
        public String showTestPage() {
            return "testPageView";
        }
    }
    

    First way to add the data is to set it as attributes on the request. The new controllers will look like this (A):

    @Controller
    public class TestController {
        @RequestMapping(value="/test")
        public String showTestPage(HttpServletRequest request) {
            request.setAttribute("param1", "foo");
            request.setAttribute("param2", "bar");
            return "forward:/test2";
        }
    }
    
    @Controller
    public class TestController2 {
        @RequestMapping(value="/test2")
        public String showTestPage(HttpServletRequest request) {
            String param1 = (String) request.getAttribute("param1");
            String param2 = (String) request.getAttribute("param2");
            return "testPageView";
        }
    }
    

    Since the view name in the forward prefix is basically an URL, you can also have the following versions (attribute changed to parameter) (B):

    @Controller
    public class TestController {
        @RequestMapping(value="/test")
        public String showTestPage() {
            return "forward:/test2?param1=foo&param2=bar";
        }
    }
    
    @Controller
    public class TestController2 {
        @RequestMapping(value="/test2")
        public String showTestPage(HttpServletRequest request) {
            String param1 = request.getParameter("param1");
            String param2 = request.getParameter("param2");
            return "testPageView";
        }
    }
    

    You can also further simplify the second controller by using annotations instead:

    @Controller
    public class TestController2 {
        @RequestMapping(value="/test2")
        public String showTestPage(@RequestParam String param1, @RequestParam String param2) {
            return "testPageView";
        }
    }
    

    And just for the fun of it, and to show Spring’s binding behavior in action, you could do it even like this (C):

    @Controller
    public class TestController {
        @RequestMapping(value="/test")
        public String showTestPage() {
            return "forward:/test2?param1=foo&param2=bar";
        }
    }
    
    @Controller
    public class TestController2 {
        @RequestMapping(value="/test2")
        public String showTestPage(@ModelAttribute DummyBinder params) {
            String param1 = params.getParam1();
            String param2 = params.getParam2();
            return "testPageView";
        }
    }
    
    class DummyBinder {
        private String param1;
        private String param2;
    
        public String getParam1() {
            return param1;
        }
    
        public void setParam1(String param1) {
            this.param1 = param1;
        }
    
        public String getParam2() {
            return param2;
        }
    
        public void setParam2(String param2) {
            this.param2 = param2;
        }
    }
    

    I would personally go with solution A for many parameters, and solution B for a few. Solution C has a sort of “huh…?!” effect so I would avoid it (also it works with parameters added to the URL so a few of those or you get a messy URL).

    Adding the data in the session would also work off course, but would extend the data’s life time unnecessarily, so the best place is to add it on the request during the transition to the second controller.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to forward-declare the HINSTANCE type from the WinAPI without including
Is there a straight forward way to view the SQL command text actually executed
Is there a trivial, or at least moderately straight-forward way to generate territory maps
In PL/SQL Developer v7.1.x, is there way way to ignore large data types in
is there a way to use request.getRequestDispatcher with a FQDN? Something like request.getRequestDispatcher(http://mysite.com/test) If
Is there a way to rewind/fast forward (or at least start playing at a
Is there way in next piece of code to only get the first record?
is there way thats i can preselect an item when the page loads or
is there way how to get name ov event from Lambda expression like with
Is there way to better identify design pattern in source codes, esp. if you

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.