全球课程访问 [英] Access session in Global class
问题描述
我非常需要我的用户对象,因此我想将其存储在缓存中.由于不知道何时将其从缓存中销毁,因此我将其作为interceptor
放置.所以我定义了以下代码:
I intensely need my user object so I thought I would store it in the Cache. Since I do not know when it is destroyed from the Cache, I though to put it as an interceptor
. So I defined the following code:
@Override
public Action<?> onRequest(Request request, Method actionMethod) {
// find the user based on the userId that is stored in the session
// scope.
String userId = session.get("user"); // does not work
User loggedInUser = (User) Cache.get(userId);
if (loggedInUser == null) {
loggedInUser = User.getUerById(userSyscode);
}
return super.onRequest(request, actionMethod);
}
我认为我可以使用:
session.get("user");
但是在我看来,就像session
无法从Global class
访问一样,我做错了吗?
But it seems to me, like the session
is not accessible from the Global class
, am I doing something wrong?
推荐答案
我遇到了类似的问题,并通过动作组合"解决了. http://www.playframework.org/documentation/2.0.1/JavaActionsComposition
I had a similar problem and solved it with "action composition". http://www.playframework.org/documentation/2.0.1/JavaActionsComposition
或者,这是您要覆盖的onRequest
的代码,所以我认为您可以做同样的事情,只需将代码放入call
方法即可.
Alternatively, this is the code for onRequest
that you are overriding, so I would think you can do the same thing and just put your code in the call
method.
public Action onRequest(Request request, Method actionMethod) {
return new Action.Simple() {
public Result call(Context ctx) throws Throwable {
/* your code here */
return delegate.call(ctx);
}
};
}
我不知道session
是否可以直接使用,但是此时您可以从ctx
变量中获取它.我认为它会像ctx.session().get("user")
.
I don't know if session
will be available directly, but at that point you can get to it from the ctx
variable. I think it'll be something like ctx.session().get("user")
.
这篇关于全球课程访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!