java Spring MVC - 请求映射,具有两个不同参数的两个 url

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6571307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 16:25:20  来源:igfitidea点击:

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

javaspringspring-mvc

提问by svlada

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

在 Spring 中是否有可能有一种方法有两个不同的 url,每种方法都有不同的参数?

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

同时 ULR1 被映射到其他一些控制器中

@RequestMethod(URL1)
public void handleAction1(@ModelAttribute("A") A a, ...) {
}

回答by Bozho

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

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

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.
  • 使用不同的网址
  • 在 misc 控制器中使用一种方法,该方法根据请求参数分派到不同的控制器(注入)。


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:

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

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

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

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

回答by Erhan Bagdemir

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.

但是如果你想为每个映射使用不同的参数,那么你必须提取你的方法。

回答by svlada

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