java 基于url从另一个控制器方法调用spring mvc控制器方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5238120/
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 10:09:06  来源:igfitidea点击:

Calling a spring mvc controller method from another controller method based on url

javaspring-mvc

提问by Fahd

I am new to this forum and have searched for my problem and although similar problems are mentioned but not the one that I am looking for. So here goes.

我是这个论坛的新手,并搜索了我的问题,虽然提到了类似的问题,但不是我正在寻找的问题。所以就到这里了。

I have a payment popup in my application that can be called from any page of application and once user submits his payment details on popup, the browser redirects to paypal site and do some checks and return to my application after user confirmation.

我的应用程序中有一个付款弹出窗口,可以从应用程序的任何页面调用,一旦用户在弹出窗口上提交他的付款详细信息,浏览器就会重定向到贝宝网站并进行一些检查,并在用户确认后返回我的应用程序。

Now in this callback method I need to forward the request to the original page which called the popup.

现在在这个回调方法中,我需要将请求转发到调用弹出窗口的原始页面。

So now I have the app url in my callback method but can't seem to forward the request to it.

所以现在我的回调方法中有应用程序 url,但似乎无法将请求转发给它。

Example like below:

示例如下:

mv = new ModelAndView("forward:/secure/music/index");

OR

或者

return "forward:/secure/music/index";

If I replace forward: with redirect: it works fine but I need to forward it, not redirect it (and hence initiate another request).

如果我用 redirect: 替换 forward: 它工作正常,但我需要转发它,而不是重定向它(因此启动另一个请求)。

回答by jelies

If you want to call a method of another controller, you could @Autowirethe second controller in your main controller and then call the desired method, passing the same request as parameter.

如果你想调用另一个控制器的方法,你可以@Autowire在主控制器中调用第二个控制器,然后调用所需的方法,将相同的请求作为参数传递。

Hope this helps, it worked for me!

希望这会有所帮助,它对我有用!

回答by Arun

You should make the return type of your calling method as ModelAndView and make the call as follows- return new ModelAndView("forward:/secure/music/index");

您应该将调用方法的返回类型设置为 ModelAndView 并按如下方式进行调用 - return new ModelAndView("forward:/secure/music/index");

It works for me, might work for you as well.

它对我有用,也可能对你有用。

回答by jny

return new ModelAndView(viewName) 

should work. However, if you need some values from model, you'll need to populate them before returning ModelAndView and return model as well.

应该管用。但是,如果您需要模型中的一些值,则需要在返回 ModelAndView 和返回模型之前填充它们。