java 如何在播放框架的会话中存储对象(不是字符串)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1694828/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 17:38:21  来源:igfitidea点击:

How to store object (not string) in session in play framework?

javasessioncachingplayframework

提问by del-boy

I have a project for school and I have to use Java. Recently I found play framework and I want to try to use it. It's easy enough for a quick start, but I ran into a problem with session.

我有一个学校项目,我必须使用 Java。最近发现了play框架,想试用一下。快速入门很容易,但我遇到了会话问题。

Being stateless by its nature, play sends entire session to user in cookie and receives it on next request, so it allows only limited amount of data in session.

由于本质上是无状态的,play 在 cookie 中将整个会话发送给用户,并在下一个请求时接收它,因此它只允许会话中的有限数据量。

What I want to do is to fetch User object from DB on user login, and keep it in session, so I could access it from templates and so on (I have some methods in User class that I need to call in order to customize UI), but if I put User object, play calls its toString method and puts that in session.

我想要做的是在用户登录时从 DB 中获取 User 对象,并将其保留在会话中,这样我就可以从模板等访问它(我需要在 User 类中调用一些方法来自定义 UI ),但是如果我放置 User 对象, play 会调用其 toString 方法并将其放入会话中。

I found that recommended way is to put larger amount of data into Cache, but I don't know how to access it from template (I guess I could make method with @Before annotation and add user to renderArgs, but that does not seem very clean to me). Another problem is that Cache has limited lifetime, so my object can disappear.

我发现推荐的方法是将大量数据放入缓存,但我不知道如何从模板访问它(我想我可以使用 @Before 注释创建方法并将用户添加到 renderArgs,但这似乎不太对我来说很干净)。另一个问题是 Cache 的生命周期有限,所以我的对象可能会消失。

Does anyone has experience with this kind of problems?

有没有人有过这种问题的经验?

Is there any way to use server side session with play? I don't need REST functionality for this project, so, as far as I am concerned, application can save state...

有什么办法可以在游戏中使用服务器端会话吗?我不需要这个项目的 REST 功能,所以,就我而言,应用程序可以保存状态......

采纳答案by Jason M

Use Java Serialization to serialize a Hashmap to a file or to the database. Name the file or id column after a unique identifier you store in the user's cookie. Then put the User object in the hashmap before you serialize the hashmap. Now you have a persistent store that you can access. When the framework forgets the User object or any other session information, you can deserialize the Hashmap. Then write some static helper methods, static Object SessionDB.get(String id, String key) and SessionDB.put(String id, String key, Object value). I use this method on my homemade framework to store session information over a small server farm. Of course I use the database, not a file system.

使用 Java 序列化将 Hashmap 序列化为文件或数据库。以您存储在用户 cookie 中的唯一标识符命名文件或 id 列。然后在序列化哈希图之前将 User 对象放入哈希图中。现在您有一个可以访问的持久存储。当框架忘记 User 对象或任何其他会话信息时,您可以反序列化 Hashmap。然后写一些静态辅助方法,静态Object SessionDB.get(String id, String key)和SessionDB.put(String id, String key, Object value)。我在我自制的框架上使用这种方法在一个小型服务器场上存储会话信息。当然我使用的是数据库,而不是文件系统。

回答by opensas

I think you should have a look at

我想你应该看看

http://groups.google.com/group/play-framework/browse_thread/thread/3d6946ad0b00303b/188e1b272d91408d?lnk=gst&q=store+object+in+session#188e1b272d91408d

http://groups.google.com/group/play-framework/browse_thread/thread/3d6946ad0b00303b/188e1b272d91408d?lnk=gst&q=store+object+in+session#188e1b272d91408d

you can also search here

你也可以在这里搜索

http://groups.google.com/group/play-framework

http://groups.google.com/group/play-framework

the play framework discussion list at google groups is very active, and you usually get a response in a couple of days, at most...

google组的play框架讨论列表很活跃,一般几天就收到回复,最多……

回答by Jay Q.

Another option is to convert your objects into JSON. I created a simple Util for this and used FlexJSON to serialise my objects into JSON.

另一种选择是将您的对象转换为 JSON。我为此创建了一个简单的 Util,并使用 FlexJSON 将我的对象序列化为 JSON。

import play.Logger;
import play.mvc.Http;
import play.mvc.Http.Session;
import flexjson.JSONDeserializer;
import flexjson.JSONSerializer;

public class SessionManager {

private static JSONSerializer s = new JSONSerializer();

    public static void addSession(String key, Object value) {

        if(value != null) {
            Session session = Http.Context.current().session();
            session.put(key, s.deepSerialize(value));
        } else {
            Logger.info("Value for " + key + " is null");
        }
    }

    public static <T> T get(String key) {

        Session session = Http.Context.current().session();
        final String value = session.get(key);

        if (value == null) {
            return null;
        }

        return new JSONDeserializer<T>().deserialize(value);
    }

}

NOTE: You'll have to manage cleaning after your work and keep in mind that since this is stored in the cookie, it is not as secure.

注意:您必须在工作后管理清洁工作,并记住,由于它存储在 cookie 中,因此它并不安全。