【问题标题】:Grails command binding - Domain classes with relations - it's possible?Grails 命令绑定 - 具有关系的域类 - 可能吗?
【发布时间】:2026-01-29 15:10:01
【问题描述】:

如果我有类似的域类:

class A {
 short aCode
 //...
 static hasMany = [bs : B]
 static mapping = {
   id column: 'a_code' name: 'aCode'
 }
}

class B {
  long bCode
  static belongsTo = [a: A]
  id column: 'b_code' name: 'bCode'
}

并尝试在命令中使用 B 类:

class SomeCommand {
  List<B> bs = ListUtils.lazyList( [], FactoryUtils.instantiateFactory(B) )
}

我对 aCode 的输入应该是:

<input type='text' name='bs[0].a.aCode' />

为什么这没有被 grails 控制器绑定? command.bs[0].a 绑定后保持为空。

我正在使用 Grails 2.0.3

【问题讨论】:

    标签: java data-binding grails


    【解决方案1】:

    我已经毫无问题地完成了关系绑定。

    我不确定这是如何工作的。

    class SomeCommand {
      List<B> bs = ListUtils.lazyList( [], FactoryUtils.instantiateFactory(B) )
    }
    
    <input type='text' name='bs[0].a.aCode' />
    

    因为,A(Parent)有很多 B(Children),B 属于 A。

    例如。 A 有 10Bs (bs[0] ~ bs[9]) 我总是可以使用这样的东西来绑定输入。

    <input type="text" name="a.aCode" />
    
    <input type="text" name="bs[0].bCode" />
    <input type="text" name="bs[1].bCode" />
    ....
    ....
    

    不确定如何将子 (B) 的数据绑定到父 (A)。

    【讨论】:

    【解决方案2】:

    实际上与命令对象和列表的数据绑定不起作用。为此有一个JIRA

    【讨论】:

      最近更新 更多