gpt4 book ai didi

GrailsDataBinder 限制?

转载 作者:行者123 更新时间:2023-12-02 14:42:50 28 4
gpt4 key购买 nike

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

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

//this works
productLineItems[0].product.id='123'
productLineItems[0].name='product name'
productLineItems[0].description='some description'
...

但是,如果我的 productLineItems集合嵌入在我试图保存的域类的关联中,然后 GrailsDataBinderorg.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。

最佳答案

请参阅 https://github.com/jeffbrown/embeddedcollectionbinding 上的示例应用程序.这是一个 Grails 2.3.7 应用程序,它演示了一种管理绑定(bind)嵌套集合的方法。以下测试使用您描述的相同嵌套参数结构,并且测试通过:

// 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'
}
}

如果您的模型中有一些其他细节使这复杂化,如果您能提供更多细节,我将很乐意提供帮助。

我希望这会有所帮助。

关于GrailsDataBinder 限制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25605546/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com