播放框架模板引擎,传递了静态成员以渲染导致NullPointerException [英] Play Framework Template Engine, static member passed to render cause NullPointerException

查看:87
本文介绍了播放框架模板引擎,传递了静态成员以渲染导致NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一段时间以来,我一直在模板中遇到NullPointerException的问题,并且无法在线找到任何帮助,所以我希望这里的人可以阐明我的问题.

I've struggled with a NullPointerException in my template for a while and haven't been able to find any help online so I'm hoping that someone here can shed some light on my issue.

我创建了自己的基本控制器",所有其他控制器都对其进行了扩展. 基本控制器包含一个静态成员,该成员保存用户会话(不是播放会话,而是数据库模型):

I have created my own "Base Controller" that all my other controllers extend. The base controller contains a static member which holds the users session (not the play session but a db model):

public class Home extends SessionController {
...
}

public class SessionController extends Controller {
    protected static SessionDao siteSession;
}

对我的问题... 我需要将此静态会话成员传递给我的模板. 在首页中,请执行以下操作:

To my problem... I need to pass this static session member to my template. In Home I do the following:

public class Home extends SessionController {

    public static void index() {
        render(siteSession);
    }
}

然后在我的模板中执行:

And then in my template I do:

${siteSession.search.query}

这会导致siteSession发生NullPointerException(我确信在将其传递给render()之前,已在Controller中对其进行了正确设置).

This causes a NullPointerException on siteSession (I am sure it is set properly in the Controller before passing it to render()).

我一直在搞乱很多,并设法使以下内容起作用:

I've been messing around quite a bit and managed to get the following to work:

public class Home extends SessionController {

    public static void index() {
        // Just setting mySession to siteSession
        SessionDao mySession = siteSession;
        render(siteSession);
    }
} 

然后在我的模板中执行:

And then in my template I do:

${mySession.search.query}

请注意,我在模板中使用mySession,但在执行操作时仍将siteSession传递给render().由于某种原因,这可行,我得到了我的会话对象,一切都很好,但这并不是真正做到这一点的方法.我什至不能访问模板中的mySession,因为它从未传递给render().

Note that I use mySession in the template and I still pass siteSession to render() in my action. For some reason this works, I get my session object and all is well but this can't really be the way to go about doing this. I shouldn't even be able to access mySession in the template since it was never passed to render().

如果有人可以对此有所了解,我将不胜感激.

If someone can shed some light on this I would much appreciate it.

我的首选方案是让SessionController覆盖渲染并将siteSession对象自动"传递给模板引擎.如果有人对我的工作方式有任何建议,请尝试并无法访问模板中的所有对象(此代码段无效),我将不胜感激:

My preferred scenario would be to let SessionController override render and pass the siteSession object to the template engine "automatically". If anyone has any suggestions on how I go about doing that I would appreciate it as I have tried and was unable to gain access to any and all objects in the template (this snippet doesn't work):

public class Home extends SessionController {
    public static void render(Object ... args) {
        if (args == null) {
        args = new Object[1];
        }
        args[args.length] = siteSession;
        Controller.render(args);
    }
}

推荐答案

当您不通过render()方法传递mySession对象时,我不确定为什么mySession对象在模板中可用关于如何使对象自动传递到模板中的问题相当简单.

I am not sure why the mySession object is available in your template when you are not passing it in through the render() method, but to answer your question on how you can get objects automatically passed into your templates, is fairly trivial.

在基本控制器中,创建一个使用@Before批注进行批注的方法,并将所需的对象添加到renderArgs对象.例如...

In your base controller, create a method that is annotated with the @Before annotation, and add the required objects to the renderArgs object. For Example...

public class SessionController extends Controller {
    protected static SessionDao siteSession;

    @Before
    static void populateRenderArgs() {
        renderArgs.put("siteSession", siteSession);
        // etc... add any other global template arguments here
    }
}

有关renderArgs对象的更多信息,请在此处检查,然后查看"将数据添加到模板范围"部分.

For more info on the renderArgs object, check here and look for the "Add data to the template scope" section.

这篇关于播放框架模板引擎,传递了静态成员以渲染导致NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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