Grails会话变量为null [英] Grails session variable null

查看:95
本文介绍了Grails会话变量为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序具有昂贵的服务方法,其结果必须为1)检查错误,以及2)通过URL(即与JavaScript变量相对)显示给Java小程序.该方法的结果是一个字符串,并且小程序仅 能够从文件或URL加载数据.

My app has an expensive service method, results of which must be 1) checked for errors and 2) presented to a Java applet via a URL (i.e. as opposed to a JavaScript variable). The method result is a string, and the applet is only capable of loading data from a file or URL.

我尝试使用会话变量来解决该问题:

I tried to deal with the problem using a session variable:

def action1 = {
    def input = params['input']
    def result = expensiveServiceMethod( input )
    def failed = result == null
    session['result'] = result
    render( view:'view1', model:[failed:failed] )
}

def action2 = {
    def result = session['result']
    render( result )
}

然后,在view1中,根据故障状态有条件地显示applet,并且applet通过action2 URL访问结果.

Then, in view1 the applet is conditionally displayed depending on the failure status, and the results are accessed by the applet via the action2 URL.

很遗憾,action2中的result正在出现null.我已经确认resultaction1中不是 null.我做错了吗?

Unfortunately, result in action2 is coming up null. I've verified that result is not null in action1. Am I doing it wrong?

注意
我本来会用flash来代替的,但是为了初始化applet,还有其他的请求.

Note
I would have used flash instead, but there are additional requests made in order to initialize the applet.

推荐答案

Applet无法自行跟踪会话Cookie.因此,当您的小程序向action2发送第二个请求时-它不会将会话cookie发送回服务器.因此,对于服务器来说,它就像一个全新的会话,您在action1期间在会话中设置的任何内容在action2中将不可用.您必须在小程序中跟踪cookie,并在拨打电话时将其发送回服务器.

Applets aren't able to track session cookies their self. So when your applet sends second request to action2 - it does't send session cookie back to server. Hence for server its like a brand new session, any thing you set in session during action1 won't be available in action2. You will have to track cookies in your applet and send them back to server when making calls.

我从未做过,但是我认为您可以在客户端(小程序)上使用Apache commons http客户端-它支持跟踪cookie

I have never done it, but I think you may use Apache commons http client on your client side (applet) - it has support for tracking cookies

参见此问题-

这篇关于Grails会话变量为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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