从 Play 2.0 Scala 控制器中的请求获取表单参数值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9105409/
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
Get form parameter value from request in Play 2.0 Scala controller
提问by amorfis
In Play 2.0 Scala application I have simple page with Form with one parameter. It redirects to another page, where I want to do something with parameter from the form. How can I get it?
在 Play 2.0 Scala 应用程序中,我有一个带有一个参数的简单页面。它重定向到另一个页面,在那里我想用表单中的参数做一些事情。我怎么才能得到它?
I'm looking for something like
我正在寻找类似的东西
request.formData.get("paramName")
I know request.body, but still don't know how to get single parameter value from it.
我知道request.body,但仍然不知道如何从中获取单个参数值。
采纳答案by andy petrella
I'd say that the easyest way to retrieve forms data is to use the Formstructure in play.api.data. So here is how you could do it in play2.0-rc1
我想说的是检索数据形式的easyest的方法是使用Form结构play.api.data。所以这里是你如何在play2.0-rc1 中做到这一点
val form = Form[(String, String)](
tuple(
"paramName1" -> nonEmptyText,
"paramName2" -> nonEmptyText
)
)
form.bindFromRequest.fold(
failure => (),//do smthg with the failure info
{ case (p1, p2) => println(p1);println(p1)}
)
Instead of using nonEmptyTextyou might use of[String].
而不是使用nonEmptyText您可以使用of[String]。
Check what is put in your hands for that mapping here Forms Helper. Some other information that should help you further are here.
在Forms Helper 中检查您手中的映射。此处提供了一些可以进一步帮助您的其他信息。
回答by user2324323
If a post request with following may work
如果具有以下内容的发布请求可能有效
request().body().asFormUrlEncoded().get("myparam")[0];
request().body().asFormUrlEncoded().get("myparam")[0];

