【问题标题】:Grails session variable nullGrails 会话变量 null
【发布时间】:2012-06-21 09:04:01
【问题描述】:

我的应用程序有一个昂贵的服务方法,其结果必须是 1) 检查错误和 2) 通过 URL(即与 JavaScript 变量相反)呈现给 Java 小程序。方法结果是一个字符串,小程序只有能够从文件或 URL 加载数据。

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

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中,小程序根据故障状态有条件地显示,结果由小程序通过action2 URL访问。

不幸的是,action2 中的result 即将出现null。我已经验证 result 是 not null in action1。我做错了吗?

注意
我会改用flash,但是为了初始化小程序,还提出了额外的请求。

【问题讨论】:

  • 我也有同样的问题,但是我的代码没有问题,我什么也没做
  • 我的问题是服务器 url,我使用 localhost 访问,但提交服务器后返回我的本地 IP。如果我直接使用我的 IP 访问,一切正常。

标签: session grails session-state session-variables grails-2.0


【解决方案1】:

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

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

看到这个question -

【讨论】:

  • 谢谢,我想我没有完全理解会话cookie机制。
猜你喜欢
  • 1970-01-01
  • 2012-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-21
  • 2014-07-19
相关资源
最近更新 更多