我非常喜欢play framework 2.3的ActionBuilder和andThen方法,它允许你动态组合动作.
这是我想要如何使用动作组合的片段:
def showHomepage = RedirectingAction andThen
AuthenticatedAction andThen NotificationAction async { request =>
Future {
Ok(views.html.homepage.render(request.user, request.notifications ))
}
}
Run Code Online (Sandbox Code Playgroud)
您可以猜到,NotificationAction依赖于AuthenticatedAction,因此需要包含User对象的AuthenticatedRequest.
代码抱怨:
object NotificationAction extends ActionBuilder[NotificationAuthRequest] {
def invokeBlock[A](request: AuthenticatedRequest[A], block: (NotificationAuthRequest[A]) => Future[Result]) = { ...
Run Code Online (Sandbox Code Playgroud)
错误是:对象创建不可能,因为类型为[A]的特征ActionFunction中的方法invokeBlock(请求:play.api.mvc.Request [A],块:controllers.v3.ScalaHomepageController.NotificationAuthRequest [A] => scala.concurrent .Future [play.api.mvc.Result])scala.concurrent.Future [play.api.mvc.Result]未定义
显然它只允许:
def invokeBlock[A](request: Request[A], block: ...
Run Code Online (Sandbox Code Playgroud)
但不是:
def invokeBlock[A](request: AuthenticatedRequest[A], block: ...
Run Code Online (Sandbox Code Playgroud)
如果有人能对此有所了解,我真的很感激.也许我的方法是错误的,但我不喜欢预先组合动作的想法(比如使用ActionFunction)因为我可能会有更多的动作,我可能会在以后混合.
这是代码:
case class AuthenticatedRequest[A](val user: Option[User], request: Request[A]) extends WrappedRequest(request)
case class NotificationAuthRequest[A](val user: Option[User], val notifications: Option[List[UserNotificationData]], request: …Run Code Online (Sandbox Code Playgroud)