且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Spring MVC-请求映射,两个带有两个不同参数的网址

更新时间:2023-02-14 16:55:56

更新:您的问题似乎完全不同.

Update: It appears your question is completely different.

否,您不能在不同的控制器中使用相同的URL和不同的参数.而且这没有多大意义-网址指定了资源或操作,并且不能在两个控制器(表示不同的行为)中以完全相同的方式命名.

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).

您有两个选择:

  • 使用其他网址
  • 在misc控制器中使用一种方法,该方法根据请求参数调度到不同的控制器(被注入).

原始答案:

不.但是,您可以使用两种方法执行相同的操作:

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);
}

如果我没有正确理解,并且您想要相同的ModelAttribute,则只需:

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

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

最后-如果需要不同的请求参数,则可以使用@RequestParam(required=false)列出所有可能的参数.

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