2
@RequestMapping({"/someurl"})
public String execute(Model model) {
    if (someCondition) {
        return "forward:/someUrlA";
    } else {
        return "forward:/someUrlB";
    }
}

Im trying to forward request from a controller to another controller. When I run this the controller returns string as response instead of forwarding to the other controller... Can someone please point out what I'm missing?

Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90
user1955934
  • 3,185
  • 5
  • 42
  • 68
  • Is your controller a `@Controller` or a `@RestController`? Also using the url as path variable (the "{}") looks wrong to me. – Evgeni Dimitrov Feb 28 '15 at 09:38
  • That should have worked. How do you have `/someUrlA` and `/someUrlB` mapped? Also, unless you are binding multiple paths to the same handler, you can specify `@RequestMapping("/someurl")`. – sh0rug0ru Oct 12 '15 at 11:17

1 Answers1

1

You can redirect request instead of forwarding.

so change your return with

    if (someCondition) {
        return "redirect:/someUrlA";
    } else {
        return "redirect:/someUrlB";
    }

For more details refer this documentation

  • 2
    A redirect will send an HTTP 302 response to the client and will force the client to make another request to the server. The question was specifically about forwarding, which is entirely server side. – sh0rug0ru Oct 12 '15 at 11:20