scala Play 框架 - 使用参数重定向

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

Play Framework - Redirect with params

scalaplayframeworkplayframework-2.0

提问by pchronz

I am trying to figure out how to do a redirect within a controller action in Play (2.0) using Scala.

我想弄清楚如何使用 Scala 在 Play (2.0) 中的控制器操作中进行重定向。

The redirect using

重定向使用

Redirect(routes.Application.index)

works just fine.

工作得很好。

What I cannot figure out from the docs, API, or Google is how to add parameters to the call.

我无法从文档、API 或 Google 中弄清楚如何向调用添加参数。

I am coming from Grails where this could be done easily as follows:

我来自 Grails,在那里这可以很容易地完成,如下所示:

redirect action: "index", params: ["key": "value"] .

重定向动作:“索引”,参数:[“键”:“值”]。

The only way I have found is to call Redirect using a string url and a query string, which seems awkward.

我发现的唯一方法是使用字符串 url 和查询字符串调用 Redirect,这看起来很尴尬。

Basically I would like to make use of Redirect(Call) somehow, but I do not how to create the Call object using reverse routing.

基本上我想以某种方式使用 Redirect(Call),但我不知道如何使用反向路由创建 Call 对象。

Am I missing something/not getting the concept in Play/Scala?

我是否遗漏了什么/在 Play/Scala 中没有得到这个概念?

Thanks in Advance!

提前致谢!

回答by biesior

Ellou'

埃卢'

A route is just a function, so you can pass arguments as usual:

路由只是一个函数,因此您可以像往常一样传递参数:

// Redirect to /hello/Bob
def helloBob = Action {
    Redirect(routes.Application.hello("Bob"))    
}

This snippet comes from http://www.playframework.org/documentation/2.0/ScalaRouting(at the bottom)

这个片段来自http://www.playframework.org/documentation/2.0/ScalaRouting(在底部)

回答by Yawo Guillaume Kpotufe

You can also avoid creating another function just for this in your controller. In your route config, you can simply add something like this:

您还可以避免在控制器中为此创建另一个函数。在您的路由配置中,您可以简单地添加如下内容:

  GET  /google  @controllers.Default.redirect(to = "http://google.com")