GrailsDataBinder限制?

GrailsDataBinder限制?,grails,data-binding,Grails,Data Binding,我试图使用grails数据绑定将一些表单参数映射到我的模型中,但我认为映射嵌入式集合可能存在一些限制 例如,如果我提交了一些类似这样的参数,那么映射工作正常: //this works productLineItems[0].product.id='123' productLineItems[0].name='product name' productLineItems[0].description='some description' ... 但是,如果我的productLineItems集

我试图使用grails数据绑定将一些表单参数映射到我的模型中,但我认为映射嵌入式集合可能存在一些限制

例如,如果我提交了一些类似这样的参数,那么映射工作正常:

//this works
productLineItems[0].product.id='123'
productLineItems[0].name='product name'
productLineItems[0].description='some description'
...
但是,如果我的
productLineItems
集合嵌入到我试图保存的域类的关联中,那么
GrailsDataBinder
就会出现
org.codehaus.groovy.grails.exceptions.InvalidPropertyException

//this blows up
sale.productLineItems[0].product.id='123'
sale.productLineItems[0].name='product name'
sale.productLineItems[0].description='some description'
...
我真的很想避免手工绘制地图。有办法解决这个问题吗

我正在使用Grails 2.3.7。

请参阅位于的示例应用程序。这是一个Grails2.3.7应用程序,它演示了一种管理绑定嵌套集合的方法。以下测试使用与您描述的相同的嵌套参数结构,并且测试通过:

// test/unit/demo/DemoControllerSpec.groovy
package demo

import grails.test.mixin.TestFor
import spock.lang.Specification

@TestFor(DemoController)
@Mock([Entry, Sale, ProductLineItem, Product])
class DemoControllerSpec extends Specification {

    void "test something"() {
        given:
        def product = new Product(code: 'initial product code').save()

        when:
        params.'sale.description' = 'some sale'
        params.'sale.productLineItems[0].product.id' = product.id
        params.'sale.productLineItems[0].name' = 'updated product name'
        params.'sale.productLineItems[0].description' = 'updated product description'
        def model = controller.createEntry()
        def entry = model.entry

        then:
        entry
        entry.sale
        entry.sale.description == 'some sale'
        entry.sale.productLineItems[0] instanceof ProductLineItem
        entry.sale.productLineItems[0].name == 'updated product name'
        entry.sale.productLineItems[0].description == 'updated product description'
        entry.sale.productLineItems[0].product
        entry.sale.productLineItems[0].product.code == 'initial product code'
    }
}
如果您的模型中有其他一些细节使问题复杂化,如果您能提供更多细节,我将很乐意提供帮助


我希望这能有所帮助。

这里没有足够的信息来确定哪里出了问题。我在下面提供了一个答案,其中包括一个指向一个项目的链接,该项目使用了与您描述的相同的嵌套param结构,并且绑定似乎在那里工作。如果你可以更改该应用程序的某些细节,从而引发问题,请随时发送请求或在此处描述更改,我可以看一看。谢谢杰夫-我现在正在尝试你的项目。我要看看它是否符合我在
GrailsDataBinder
中遇到的代码行,我忽略了您使用的是
GrailsDataBinder
。这是旧的基于Spring的活页夹。您使用Spring活页夹而不是较新的Grails活页夹有什么原因吗?这可能是你的应用程序和下面的演示之间的显著差异。弹簧夹不再维护或支撑。为了向后兼容,它留在了Grails2.3.x中。当我们升级到2.3时,我们发现当我们尝试使用新的数据绑定器时,很多东西都坏了。这只是我们尚未着手解决的许多问题之一……是的,当我将您的演示项目切换到使用遗留活页夹时,我可以重现我在应用程序中看到的相同问题。我想现在也许是考虑升级的好时机……正如在最初问题下面的评论中所描述的,这个场景是有效的,并且由Grails 2.3引入的较新的数据绑定器支持,不适用于不推荐使用的旧绑定器。遗留活页夹不再维护,我不希望我们会对此进行调查并在此时修复它。前进的方向是使用新的数据绑定器,如上面链接的演示应用程序所示。