播放框架2:在控制器之间传递Java对象 [英] Play framework 2: Passing java objects between controllers

查看:97
本文介绍了播放框架2:在控制器之间传递Java对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java用户对象,我想将其从我的Login控制器传递到Dashboard控制器.香港专业教育学院读了一堆,我尝试以下实现

I have a java User object which i want to pass from my Login controller to my Dashboard controller. ive read a bunch and im trying the following implementation

public static Result authenticate(){
    Form<LoginForm> loginform = Form.form(LoginForm.class).bindFromRequest();
    if(loginform.hasErrors()){
        return badRequest(login.render(loginform));
    }
    else{
        session().clear();
        AppUser user = AppUser.getUserByEmail(loginform.get().email);
        Context.current().args.put("user", user);
        return redirect(routes.DashBoard.index());
    }

和我的仪表板控制器

public static Result index(){
    AppUser user = (AppUser) Context.current().args.get("user");
    return ok(views.html.dashboard.index.render(user));
}

这将导致nullpointerexception

this results in nullpointerexception

这是因为两个请求当然不是同一请求.

this is because the two requests are not the same request of course.

如何通过游戏友好的方式解决此问题 就我在文档中看到的方式而言,有关动作组合的一些信息 但是我不明白.

how to solve this issue the a play friendly way By the way ive seen in the documentation something about action composition but i did not understand it.

推荐答案

尽管我同意Mauno的回答,但我想补充一点,可以使用

Though I agree with Mauno's answer, I wanted to add that it is possible to use the cache API in play to do what you are asking.

要记住的一件事是,多个用户之间的缓存是相同的.如果您希望能够从缓存中访问特定的用户对象,则必须为其具有唯一的ID.您还将在请求之间存储该ID.玩游戏是一种简单的方法.

One thing to keep in mind is that the cache is the same across multiple users. If you want to be able to access a particular user object from the cache, you're going to have to have a unique id for it. You're also going to store that id between requests. A play session is an easy way to do this.

这是一个例子

public static Result authenticate(){
    Form<LoginForm> loginform = Form.form(LoginForm.class).bindFromRequest();
    if(loginform.hasErrors()){
        return badRequest(login.render(loginform));
    }
    else{
        session().clear();
        AppUser user = AppUser.getUserByEmail(loginform.get().email);


        String uuid= java.util.UUID.randomUUID().toString();
        Cache.set(uuid,user);
        //store uuid in session for extracting the proper user from cache later
        session("uuid",uuid); 

        return redirect(routes.DashBoard.index());
    }


public static Result index(){
    //gather uuid stored in session (cookies)
    String uuid = session("uuid") 
    AppUser user = Cache.get(uuid);

    return ok(views.html.dashboard.index.render(user));
}

这些对象的寿命也无法预测,因此您必须确保检查它们的存在并可能重新创建对象.

Also these objects won't have a predictable lifespan so you're going to have to make sure you check for their existence and potentially recreate the objects.

编辑:我不确定最后一条语句,在playframework 1中,如果一段时间内不使用缓存中的对象,则它们将被删除,但是

edit: I am unsure of this last statement, in playframework 1 the objects in the cache would be removed if they weren't used for a while, however it appears from this post that if you don't specify a timeout in playframework 2, they may persist indefinitely by default. It really depends on the configuation/module being used with the play api.

针对您所描述的情况,我不建议使用此解决方案.数据库是存储此信息的合理位置,您只需在每个请求上检索用户数据即可.

I wouldn't recommend this solution for the situation you've described. The database is a reasonable place to store this information, you should just retrieve the user data on each request.

只想说可以缓存对象.

这篇关于播放框架2:在控制器之间传递Java对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆