播放框架2.0:将值存储在Http.Context中 [英] Play framework 2.0: Store values in Http.Context

查看:88
本文介绍了播放框架2.0:将值存储在Http.Context中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在play框架的scalaquery中实现基于请求"的会话.我使用scalaquery创建了一个会话,并尝试将其存储在当前的http上下文中,如下所示:

I'm trying to implement "request based" sessions in scalaquery in the play framework. I create a session using scalaquery and attempt to store it in the current http context, as follows:

def withTransaction[A](bp: BodyParser[A])(f: Request[A] => Result): Action[A] = {
   Action(bp) {
     request =>
       val context = Http.Context.current()
       val session = createSession()
       session.conn.setAutoCommit(false)
       context.args.put("scalaquery.session", session)
       try {
         val result = f(request)
         session.conn.commit()
         result
       }
       catch {
         case t: Throwable =>
           session.conn.rollback()
           throw t
       }
       finally {
         session.close()
         context.args.remove("scalaquery.session")
       }
   }
}

然后,我将动作包装在控制器中,例如:

then i wrap my actions in my controllers like:

withTransaction(parse.anyContent) {
    Action {
       //code that produces a result here
    }
}

但是,它在以下行中崩溃:

However, it crashes in the following line saying:

val context = Http.Context.current() 
[RuntimeException: There is no HTTP Context available from here.] 

那么,为什么上下文不可用?该代码直接由框架调用,因此不应在执行该代码时设置上下文吗?还是我使用错误的方式访问上下文?

So, why is the context not available? This code is called directly by the framework, so shouldn't the context be set by the time this code executes? Or am i using the wrong way for accessing the context?

会话"的类型为org.scalaquery.session.Session.我之所以要在HttpContext中进行设置,是为了使被包装的操作可以以"http范围"方式访问它,即每个请求分别存储其会话,而所有需要会话的服务都可以在公共位置找到它.每个请求分隔的范围.

The "session" is of type org.scalaquery.session.Session. The reason why i want to set it in the HttpContext is so that the wrapped actions can access it in an "http scoped" fashion, i.e. That each request stores their session separately, yet all services that need a session can find it in a public scope that is separated per request.

推荐答案

我认为问题在于您正在将Java API与Scala控制器一起使用.仅在使用Java控制器时设置Http.Context.您是否考虑过使用 Scala会话API ?

I think the problem is you're using the Java API with the Scala controller. Http.Context is only set if you're using the Java controller. Have you considered using the Scala Session API?

另外,另一个问题是,为什么您需要将会话存储在上下文中?我看到您还是将其最后删除了.如果您需要子操作能够访问会话,则只需在函数中传递它即可.

Also, another question is, why do you need to store the session in the context? I see you just remove it at the end anyway. If what you need is for the sub-actions to be able to access the session, you could just pass it in the function.

我只是假设session的类型为Session

def withTransaction[A](bp: BodyParser[A])(f: Session => Request[A] => Result): Action[A] = {
   Action(bp) {
     request =>
       val session = createSession()
       session.conn.setAutoCommit(false)
       try {
         val result = f(session)(request)
         session.conn.commit()
         result
       }
       catch {
         case t: Throwable =>
           session.conn.rollback()
           throw t
       }
       finally {
         session.close()
       }
   }
}

您的子操作将会是

withTransaction(parse.anyContent) { session => request =>
    //code that produces a result here
}

您不再需要将其包装在Action中,因为它已经被withTransaction

you don't need to wrap this in Action anymore since it's already wrapped by withTransaction

这篇关于播放框架2.0:将值存储在Http.Context中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆