documentation on ActionBuilder包含三个节点的管道:身份验证,添加信息,验证步骤。
我想在身份验证步骤中设置会话值。我的意思是来自.withSession
Ok(_).withSession(_)
import play.api.mvc._
class UserRequest[A](val username: Option[String], request: Request[A]) extends WrappedRequest[A](request)
class UserAction @Inject()(val parser: BodyParsers.Default)(implicit val executionContext: ExecutionContext)
extends ActionBuilder[UserRequest, AnyContent] with ActionTransformer[Request, UserRequest] {
def transform[A](request: Request[A]) = Future.successful {
new UserRequest(request.session.get("username"), request)
}
}
答案 0 :(得分:1)
您需要执行操作合成以将值添加到请求会话中,如下所示:
object WithSession extends ActionBuilder[Request] {
def invokeBlock[A](request: Request[A], block: (Request[A]) => Future[Result]) = {
block(request).map(_.withSession("key" -> "value"))
}
}
在您的控制器中:
def index = WithSession {
Ok("result")
}