事實上,如果你看看Action
代碼:https://github.com/playframework/playframework/blob/master/framework/src/play/src/main/scala/play/api/mvc/Action.scala
val echo = Action { request =>
Ok("Got request [" + request + "]")
}
這需要的Action
的apply
方法確實返回一個新Action
:
/**
* Constructs an `Action` with default content.
*
* For example:
* {{{
* val echo = Action { request =>
* Ok("Got request [" + request + "]")
* }
* }}}
*
* @param block the action code
* @return an action
*/
final def apply(block: R[AnyContent] => Result): Action[AnyContent] =
apply(BodyParsers.parse.anyContent)(block)
然後:
/**
* Constructs an `Action`.
*
* For example:
* {{{
* val echo = Action(parse.anyContent) { request =>
* Ok("Got request [" + request + "]")
* }
* }}}
*
* @tparam A the type of the request body
* @param bodyParser the `BodyParser` to use to parse the request body
* @param block the action code
* @return an action
*/
final def apply[A](bodyParser: BodyParser[A])(block: R[A] => Result): Action[A] =
async(bodyParser) { req: R[A] =>
block(req) match {
case simple: SimpleResult => Future.successful(simple)
case async: AsyncResult => async.unflatten
}
}
I ñ事實上,後來,該框架將調用的其他apply
方法Action
你通過傳遞Request
參數創建:
/**
* Invokes this action.
*
* @param request the incoming HTTP request
* @return the result to be sent to the client
*/
def apply(request: Request[A]): Future[SimpleResult]
例如:
echo(request)
這種方法,然後返回一個Result
,您之前在
val echo = Action { request =>
Ok("Got request [" + request + "]")
}
我希望我已經夠清楚了!
@vptheron我想是因爲我沒有完全理解語言的主要概念,但我不能從不同的角度解決這個問題。 Jeeez,當你知道**時,它總是顯而易見的。當你甚至不知道正確的問題時,基本上就像在黑暗中摸索。我想你已經知道了Scala語言,然後出來了你母親的子宮嗎?給我一個休息時間。我試圖讓我的腦海裏浮現一些對我來說很陌生的複雜而抽象的概念。 – Zuriar