9

Is it possible in Spring to have one method with two different urls with different params for each method?

Below is pseudo code

@RequestMethod(URL1-param1, URL2-param2)
public void handleAction(@ModelAttribute("A") A a, ...) {
}

At the same time ULR1 is mapped in some other Controller as

@RequestMethod(URL1)
public void handleAction1(@ModelAttribute("A") A a, ...) {
}
Stu Thompson
  • 38,370
  • 19
  • 110
  • 156
svlada
  • 3,218
  • 2
  • 25
  • 36

3 Answers3

14

Update: It appears your question is completely different.

No, you can't have the same url with different parameters in different controllers. And it doesn't make much sense - the url specifies a resource or action, and it cannot be named exactly the same way in two controllers (which denote different behaviours).

You have two options:

  • use different URLs
  • use one method in a misc controller that dispatches to the different controllers (which are injected) depending on the request param.

Original answer:

No. But you can have two methods that do the same thing:

@RequestMethod("/foo")
public void foo(@ModelAttribute("A") A a) {
    foobar(a, null);
}

@RequestMethod("/bar")
public void bar(@ModelAttribute("B") B b) {
    foobar(null, b);
}

If I haven't understood correctly, and you want the same ModelAttribute, then simply:

@RequestMapping(value={"/foo", "/bar"})

And finally - if you need different request parameters, you can use @RequestParam(required=false) to list all possible params.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • @svlada you avoid it by placing it in the `foobar` method. But do you need different model attributes, or you need just different urls? – Bozho Jul 04 '11 at 12:21
  • I need different URLs with different parameters. – svlada Jul 04 '11 at 12:22
  • but different request parameters, or different model attributes? – Bozho Jul 04 '11 at 12:26
  • Different parameters. But URL1 pattern must be present in two different controllers. – svlada Jul 04 '11 at 13:06
  • Then use `@RequestParam(required=false)` – Bozho Jul 04 '11 at 13:19
  • Well in Spring it is not possible that SAME url is present in two Different controllers :) – svlada Jul 04 '11 at 13:21
  • @svlada - I understood that you need _different_ urls. If you need the same url, then have one controller with multiple @RequestParam(required=false) – Bozho Jul 04 '11 at 13:38
  • Well that is problem. I cannot couple logic in one controller. Somehow i have to find way to map on url to two controllers, based on different parameters. – svlada Jul 04 '11 at 13:47
  • it is still unclear what you are trying to achieve. One or two urls? The question titles says "two urls" – Bozho Jul 04 '11 at 13:55
  • Maybe my question is not formulated well. I have one url and two controllers. One url must be mapped in both controllers but with different parameters. – svlada Jul 04 '11 at 13:59
2

you can supply multiple mappings for your handler like this

@RequestMapping(value={"", "/", "welcome"})
public void handleAction(@ModelAttribute("A") A a, ...) { }

But if you want to use different parameters for each mapping, then you have to extract your method.

Erhan Bagdemir
  • 5,231
  • 6
  • 34
  • 40
0

Something like this

@RequestMapping(value={"URL1"}, method=RequestMethod.POST)
public String handleSubmit(@ModelAttribute("A") A command, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    return helperSubmit();
}

@RequestMapping(value={"URL2"}, method=RequestMethod.POST)
public String handleSubmit(@ModelAttribute("A") A command, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    return helperSubmit();
}

private helperSubmit() {
  return "redirect:" + someUrl;
}
abalogh
  • 8,239
  • 2
  • 34
  • 49
svlada
  • 3,218
  • 2
  • 25
  • 36