如何从scala播放中访问帖子数据?

use*_*853 17 scala playframework

我有一个类型为"POST"的路由.我正在将帖子数据发送到页面.如何访问该帖子数据.例如,在PHP中使用$ _POST

如何在scala和play框架中访问帖子数据?

Leo*_*nya 9

从Play 2.1开始,有两种方法可以获得POST参数:

1)通过Action解析器参数将body声明为form-urlencoded,在这种情况下,request.body会自动转换为Map [String,Seq [String]]:

def test = Action(parse.tolerantFormUrlEncoded) { request =>
    val paramVal = request.body.get("param").map(_.head)
}
Run Code Online (Sandbox Code Playgroud)

2)通过调用request.body.asFormUrlEncoded来获取Map [String,Seq [String]]:

def test = Action { request =>
    val paramVal = request.body.asFormUrlEncoded.get("param").map(_.head)
}
Run Code Online (Sandbox Code Playgroud)


bie*_*ior 5

在这里你可以很好地了解它在Play中是如何完成的:

https://github.com/playframework/Play20/blob/master/samples/scala/zentasks/app/controllers/Application.scala

val loginForm = Form(
  tuple(
    "email" -> text,
    "password" -> text
  ) verifying ("Invalid email or password", result => result match {
    case (email, password) => User.authenticate(email, password).isDefined
  })
)



/**
 * Handle login form submission.
 */
def authenticate = Action { implicit request =>
  loginForm.bindFromRequest.fold(
    formWithErrors => BadRequest(html.login(formWithErrors)),
    user => Redirect(routes.Projects.index).withSession("email" -> user._1)
  )
}
Run Code Online (Sandbox Code Playgroud)

它在表单提交的文档中描述