1

私は遊びを学び始めています。

そして、私は Action がどのように実装されているかを理解しようとしています。私は言語構造を理解していないだけで、本当にイライラします.....

どのように書くことができるのかわかりませんか?

val echo = アクション { リクエスト => Ok("リクエストを受け取りました [" + リクエスト + "]") }

そして、それをコンパイルします....それはどのような構造ですか?それは、関数をパラメーターとして取るケースクラスになるでしょう、私は持っているかもしれません.....

しかし、ここにhttps://github.com/playframework/playframework/blob/master/framework/src/play/src/main/scala/play/api/mvc/Action.scalaのアクションの定義があります

基本的に、Action は、Essential アクションから取得された適用とそれ自体が定義するものによって、requestHeader または Request のいずれかを取る関数オブジェクト Trait であると述べています....

/**
 * An `EssentialAction` underlies every `Action`. Given a `RequestHeader`, an
 * `EssentialAction` consumes the request body (an `Array[Byte]`) and returns
 * a `Result`.
 *
 * An `EssentialAction` is a `Handler`, which means it is one of the objects
 * that Play uses to handle requests.
 */
trait EssentialAction extends (RequestHeader => Iteratee[Array[Byte], Result]) with Handler {

  /**
   * Returns itself, for better support in the routes file.
   *
   * @return itself
   */
  def apply() = this

}

/**
 * Helper for creating `EssentialAction`s.
 */
object EssentialAction {

  def apply(f: RequestHeader => Iteratee[Array[Byte], Result]): EssentialAction = new EssentialAction {
    def apply(rh: RequestHeader) = f(rh)
  }
}

/**
 * An action is essentially a (Request[A] => Result) function that
 * handles a request and generates a result to be sent to the client.
 *
 * For example,
 * {{{
 * val echo = Action { request =>
 *   Ok("Got request [" + request + "]")
 * }
 * }}}
 *
 * @tparam A the type of the request body
 */
trait Action[A] extends EssentialAction {

  /**
   * Type of the request body.
   */
  type BODY_CONTENT = A

  /**
   * Body parser associated with this action.
   *
   * @see BodyParser
   */
  def parser: BodyParser[A]

  /**
   * Invokes this action.
   *
   * @param request the incoming HTTP request
   * @return the result to be sent to the client
   */
  def apply(request: Request[A]): Future[Result]

  def apply(rh: RequestHeader): Iteratee[Array[Byte], Result] = parser(rh).mapM {
    case Left(r) =>
      Play.logger.trace("Got direct result from the BodyParser: " + r)
      Future.successful(r)
    case Right(a) =>
      val request = Request(rh, a)
      Play.logger.trace("Invoking action with request: " + request)
      Play.maybeApplication.map { app =>
        play.utils.Threads.withContextClassLoader(app.classloader) {
          apply(request)
        }
      }.getOrElse {
        apply(request)
      }
  }(executionContext)

私はそれを自分で再現することはできません.コンパイルされない単純なケースでは、それは非常に奇妙です....

4

2 に答える 2

2

確認する必要があるのは、Action オブジェクトとそれが継承するこのapplyメソッドです。

于 2014-06-05T19:44:50.280 に答える