简体   繁体   English

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

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

I have a project for school and I have to use Java. 我有一个学校项目,我必须使用Java。 Recently I found play framework and I want to try to use it. 最近我找到了play框架,我想尝试使用它。 It's easy enough for a quick start, but I ran into a problem with session. 这很容易快速启动,但我遇到了会话问题。

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. 我想要做的是在用户登录时从数据库中获取User对象,并将其保存在会话中,这样我就可以从模板中访问它等等(我在User类中有一些方法需要调用以便自定义UI ),但如果我放置User对象,则调用其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). 我发现推荐的方法是将大量数据放入Cache中,但我不知道如何从模板中访问它(我想我可以用@Before注释创建方法并将用户添加到renderArgs,但这似乎不太给我干净) Another problem is that Cache has limited lifetime, so my object can disappear. 另一个问题是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功能,因此,就我而言,应用程序可以保存状态...

Use Java Serialization to serialize a Hashmap to a file or to the database. 使用Java序列化将Hashmap序列化为文件或数据库。 Name the file or id column after a unique identifier you store in the user's cookie. 将文件或id列命名为存储在用户cookie中的唯一标识符。 Then put the User object in the hashmap before you serialize the hashmap. 然后在序列化hashmap之前将User对象放在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. 当框架忘记User对象或任何其他会话信息时,您可以反序列化Hashmap。 Then write some static helper methods, static Object SessionDB.get(String id, String key) and SessionDB.put(String id, String key, Object value). 然后编写一些静态辅助方法,静态Object SessionDB.get(String id,String key)和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. 当然我使用的是数据库,而不是文件系统。

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... 谷歌小组的游戏框架讨论列表非常活跃,你通常会在几天内收到回复,最多...

Another option is to convert your objects into JSON. 另一种选择是将对象转换为JSON。 I created a simple Util for this and used FlexJSON to serialise my objects into 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中,因此不太安全。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM