Grails 1.3.6 - 对象字段似乎失去了它们的价值

时间:2012-08-07 17:19:00

标签: variables grails

我是Grails的新手并使用古老版本的Grails(1.3.6 / 8)。我有一个带有一些变量的对象,这些变量没有保留我分配它们的东西。

class NiftyController {

    try
    {
        SomeGrid someGrid = new SomeGrid()
        def selectedDate = params.specifiedDate
        ...
        someGrid.selectedDate = selectedDate
        someGrid.longDate = Calendar.getInstance().getTimeInMillis()
        println someGrid.selectedDate // prints, say, 08/06/2012
        println someGrid.longDate // prints, say, 1302558890256
        ....
        doSomeWork(someGrid)
    }


    def doSomeWork = { SomeGrid someGrid ->
        println someGrid.selectedDate // prints '' (empty)
        println someGrid.longDate // prints 8 - the number for the current month.
    }
}

我完全摸不着头脑 - 我使用Java,这种类型的分配变量数据丢失是新的,对我来说根本没有意义。任何人都可以弄清楚这里发生了什么?我猜这是Grails特有的一种可见性问题,但我不能指出它。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

如果doSomeWork本身不需要作为控制器动作,我会将其作为普通方法而不是闭包:

private doSomeWork(SomeGrid someGrid) {
  //...
}

将它作为闭包,Grails可能会将SomeGrid参数视为命令对象,并尝试直接从params对其进行数据绑定。