Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC - Request mapping, two urls with two different parameters

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, ...) {
}
like image 248
svlada Avatar asked Jul 04 '11 12:07

svlada


2 Answers

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.

like image 160
Bozho Avatar answered Oct 19 '22 22:10

Bozho


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.

like image 28
Erhan Bagdemir Avatar answered Oct 19 '22 23:10

Erhan Bagdemir