Spring MVC-请求映射,两个带有两个不同参数的网址 [英] Spring MVC - Request mapping, two urls with two different parameters

查看:86
本文介绍了Spring MVC-请求映射,两个带有两个不同参数的网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Spring中,是否可能有一个方法,其中每个方法的两个带有不同参数的不同URL?

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

下面是伪代码

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

与此同时,ULR1在其他一些控制器中的映射为

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

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

推荐答案

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

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.

这篇关于Spring MVC-请求映射,两个带有两个不同参数的网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆