Scala Play 2,将请求传递给方法 [英] Scala Play 2, passing request to method

查看:66
本文介绍了Scala Play 2,将请求传递给方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Play 2.0应用

TestController.scala

def foo(p1: String) = Action {implicit request =>
  Ok(bar(p1))
}

private def bar(p1: String) = {
//access request parameter here
}

是否可以使用implicitrequest传递给bar

Is there a way to use implicit to pass request to bar

推荐答案

是的,您可以:

  def foo(p1: String) = Action { implicit request =>
    Ok(bar(p1))
  }

  private def bar(p1: String)(implicit req: RequestHeader) = "content"

代码:

Action { implicit request

在Action对象上调用此方法:

Calls this method on the Action object:

def apply(block: Request[AnyContent] => Result): Action[AnyContent] = {

因此,您所说的请求"与名为"block"的参数匹配.这里的隐式"是可选的:它将请求"值作为隐式参数提供给其他方法/函数调用.

So, what you called "request" matches the paramater named "block". The "implicit" here is optional: it makes the "request" value available as an implicit parameter to other method/function calls.

"bar"函数中的隐式表示可以从隐式值中获取"req"的值,而不必显式传递.

The implicit in your "bar" function says that it can take the value of "req" from an implicit value, and doesn't necessarily need to be passed in explicitly.

这篇关于Scala Play 2,将请求传递给方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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