我非常需要我的用户对象,所以我想我会将它存储在Cache中.因为我不知道什么时候它从Cache中被销毁,所以我把它作为一个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);
}
Run Code Online (Sandbox Code Playgroud)
我以为我可以用:
session.get("user");
Run Code Online (Sandbox Code Playgroud)
但在我看来,就像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);
}
};
}
Run Code Online (Sandbox Code Playgroud)
我不知道是否session可以直接使用,但此时你可以从ctx变量中获取它.我想它会是这样的ctx.session().get("user").
| 归档时间: |
|
| 查看次数: |
3170 次 |
| 最近记录: |