命令对象数据绑定 [英] command object data binding
问题描述
我尝试按以下方式使用命令对象:
I'm trying to use command objects in the following way:
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看起来像:
The GSP looks like:
<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
属性(由复选框表示)绑定到命令对象,但名称
和地址
不是。我也尝试将文本字段名称重命名为 tempBeneficiaries.beneficiary [0] .name
无济于事。
The save
property of the command object (represented by the checkbox) is bounded to the command object, but the name
and address
are not. I have also tried renaming the text field names to tempBeneficiaries.beneficiary[0].name
to no avail.
关于如何在命令对象中捕获 name
和 address
的任何想法?
Any ideas on how to capture name
and address
into the command object?
推荐答案
从命令对象的层次结构中,字段的名称应该是 tempBeneficiaries [0] .beneficiary.name
和 tempbeneficiaries [0] .beneficiary.address
From the hierarchy of your command object the name of the field should be tempBeneficiaries[0].beneficiary.name
and tempBeneficiaries[0].beneficiary.address
这是我以前用来做某事的LazyList语法类似,但我不知道你的语法是否是一个问题:
This is the LazyList syntax I've used before to do something similar, but I don't know if your syntax is an issue at all:
List<Beneficiary> copies = ListUtils.lazyList([], FactoryUtils.instantiateFactory(Beneficiary))
另外,
Contractor beneficiary = new Contractor()
$您的受益人
类中的
$ b
,以便在创建对象时创建对象。
in your Beneficiary
class so that an object is available at object creation time to be populated.
这篇关于命令对象数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!