我强烈地需要我的用户对象,所以我想我会把它保存在缓存中。 自从我当它从缓存中被破坏不知道做什么,我虽然把它作为一个interceptor
。 所以我定义了下面的代码:
@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
,我是不是做错了什么?
我有一个类似的问题,并与“行动组成”解决了这个问题。 http://www.playframework.org/documentation/2.0.1/JavaActionsComposition
或者,这是代码onRequest
,你是压倒一切的,所以我想你可以做同样的事情,只是把你的代码中call
方法。
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")
Scala中工作的代码:
override def onRouteRequest(request: RequestHeader): Option[Handler] = {
if(!request.session.get("email").isEmpty){
//I have session with email
request.session.apply("email") //this is for getting session
}
}
request.session.get返回选项[字符串]
request.session.apply返回字符串
游戏框架2阶Play.api.mvc.Session