代码之家  ›  专栏  ›  技术社区  ›  Myk

在Play Framework中从自定义scala操作提供静态页面

  •  1
  • Myk  · 技术社区  · 12 年前

    我刚接触scala,但在Java中使用play框架方面有一些经验。我已经添加了SecureSocial身份验证库,它定义了SecuredACtion,并且似乎工作正常。然而,我很难理解scala代码中自定义操作中的预期内容。

    这是我的控制器类。理想情况下,“index”只是将经过身份验证的请求以某种方式重定向到“unprotectedIndex”,但这似乎是不可能的。所以,如果不是,那么下一个最好的方法就是直接从安全操作内部提供文件,但这也不起作用。

    我的代码中缺少什么?

    object Application extends Controller with securesocial.core.SecureSocial {
      // this doesn't compile, but it's a long scala exception that I don't know how to fix.
      def index = SecuredAction { implicit request =>
        Assets.at("/public", "index.html").apply(request) 
      }
    
      def unprotectedIndex = Assets.at("/public", "index.html")
    
    }
    

    看起来它期待着一个SimpleResult,但却得到了一个Future[SimpleResult]——这感觉不应该很复杂,但我缺少了什么?

    2 回复  |  直到 12 年前
        1
  •  1
  •   serejja    12 年前

    看起来您正在使用play框架2.2。有一些更改,大多数方法返回 Future[SimpleResult] 而不仅仅是 Result SimpleResult 。您可以检查是否能够这样做: def index = SecuredAction.async {...} (但我几乎肯定你不能)。

    您可以使用此方法使其正确工作:

    import scala.concurrent.Await
    import scala.concurrent.duration._
    
    def index = SecuredAction { implicit request =>
      Await.result(Assets.at("/public", "index.html").apply(request), 5 seconds) //you can specify you maximum wait time here
    }
    

    编辑

    还有一件事需要简化:

    Await.result(unprotectedIndex(request), 5 seconds)
    

    所以你可以打电话给 unprotectedIndex 从您的 index 行动

        2
  •  0
  •   Myk    12 年前

    因此,仅通过查看IDE中的语法高亮显示,我就能够得到一些似乎可以编译和工作但看起来非常错误的东西。

    我把它改成了这样:

    def index = SecuredAction { implicit request =>
        Assets.at("/public", "index.html").apply(request).value.get.get
      }
    

    这是正确的做法吗?这对我来说真的很奇怪,我只是不熟悉这些成语吗?