我正在使用Scala Play应用程序并需要许多Controller操作,通过在Response的HTTP标头中设置参数来禁用浏览器的缓存。我决定创建一个NoCache
复合动作,因为我也使用Deadbolt-2(需要一个Deadbolt-2' s AuthenticatedRequest[_]
),它看起来像这样:
package action
import be.objectify.deadbolt.scala.AuthenticatedRequest
import play.api.http.HeaderNames
import play.api.mvc._
import scala.concurrent.Future
import scala.util.Success
case class NoCache[A](action: Action[A]) extends Action[A] with HeaderNames {
def apply(request: AuthenticatedRequest[A]): Future[Result] = {
action(request).andThen {
case Success(result) => result.withHeaders(
(CACHE_CONTROL -> "no-cache, no-store, must-revalidate"),
(PRAGMA -> "no-cache"),
(EXPIRES -> "0")
)
}
}
lazy val parser = action.parser
}
然后它不会编译尝试将此Action混合到我的Controller动作实现中,例如
def link = deadbolt.SubjectPresent()() andThen NoCache() { implicit request =>
或
def link = NoCache(deadbolt.SubjectPresent()()) { implicit request =>
但无法看到如何撰写它们......
答案 0 :(得分:0)
我找到了如何为一个动作做到这一点:
def index = NoCache {
deadbolt.WithAuthRequest()() { implicit request =>
Future {
Ok(views.html.index(userService))
}
}
}
但是,我还没有找到如何将NoCache
应用于整个控制器类。