命令对象数据绑定

时间:2011-12-06 03:08:46

标签: grails

我正在尝试以下列方式使用命令对象:

class BeneficiaryCommand {
    List<Beneficiary> tempBeneficiaries = org.apache.commons.collections.list.LazyList.decorate(new ArrayList(), new org.apache.commons.collections.functors.InstantiateFactory(Beneficiary.class))

    static constraints = {
        tempBeneficiaries(validator: {
            it.each{println it.beneficiary} // it.beneficiary is null, but I expect it to have name and address from the form
        })
    }
}

class Beneficiary {
    Contractor beneficiary // Contractor has name and address properties, both non-blank and non-nullable
    Boolean save
}

GSP看起来像:

<tr class="prop">
    <td valign="top" class="name">
      <label for="beneficiaries"><g:message code="contract.beneficiaries.label" default="New Beneficiary Name" /></label>
    </td>
    <td valign="top" class="value ${hasErrors(bean: i, field: 'name', 'errors')}">
        <g:textField name="tempBeneficiaries[0].name" />
    </td>
</tr>

<tr class="prop">
    <td valign="top" class="name">
      <label for="beneficiaries"><g:message code="contract.beneficiaries.label" default="New Beneficiary Address" /></label>
    </td>
    <td valign="top" class="value ${hasErrors(bean: i, field: 'address', 'errors')}">
        <g:textField name="tempBeneficiaries[0].address" />
    </td>
</tr>

<tr class="prop">
    <td valign="top" class="name">
        </td>
    <td valign="top" class="value ${hasErrors(bean: beneficiaryCommand, field: 'save', 'errors')}">
        <div id="newBeneficiaryFlagDisplay"><g:checkBox name="tempBeneficiaries[0].save" /> Create new Beneficiary</div>
    </td>
</tr>

命令对象的save属性(由复选框表示)与命令对象绑定,但nameaddress不是。我也尝试将文本字段名称重命名为tempBeneficiaries.beneficiary[0].name,但无济于事。

关于如何将nameaddress捕获到命令对象中的任何想法?

1 个答案:

答案 0 :(得分:4)

从命令对象的层次结构中,字段的名称应为tempBeneficiaries[0].beneficiary.nametempBeneficiaries[0].beneficiary.address

这是我之前用过的类似的LazyList语法,但我不知道你的语法是否是一个问题:

List<Beneficiary> copies = ListUtils.lazyList([], FactoryUtils.instantiateFactory(Beneficiary))

另外,您可以尝试

Contractor beneficiary = new Contractor()

Beneficiary类中,以便在对象创建时可以填充对象。