Grails命令对象数据绑定 [英] Grails command object data binding
问题描述
Grails已将请求参数绑定到域对象,并将其关联。这主要取决于检测以 .id
结尾的请求参数,并自动从数据库加载。
Grails has very good support for binding request parameters to a domain object and it's associations. This largely relies on detecting request parameters that end with .id
and automatically loading those from the database.
但是,不清楚如何填充命令对象的关联。以下示例:
However, it's not clear how to populate the associations of a command object. Take the following example:
class ProductCommand {
String name
Collection<AttributeTypeCommand> attributeTypes
ProductTypeCommand productType
}
此对象具有单端关联 ProductTypeCommand
和与 AttributeTypeCommand
的多端关联。所有属性类型和产品类型的列表都可以从该接口的实现获得。
This object has a single-ended association with ProductTypeCommand
and a many-ended association with AttributeTypeCommand
. The list of all attribute types and product types are available from an implementation of this interface
interface ProductAdminService {
Collection<AttributeTypeCommand> listAttributeTypes();
Collection<ProductTypeCommand> getProductTypes();
}
我使用这个界面在GSP中填充产品和属性类型选择列表。我也依赖 - 将此接口注入到命令对象中,并使用它来模拟 attributeTypes
和 productType
属性命令对象
I use this interface to populate the product and attribute type selection lists in a GSP. I also dependency-inject this interface into the command object, and use it to "simulate" attributeTypes
and productType
properties on the command object
class ProductCommand {
ProductAdminService productAdminService
String name
List<Integer> attributeTypeIds = []
Integer productTypeId
void setProductType(ProductTypeCommand productType) {
this.productTypeId = productType.id
}
ProductTypeCommand getProductType() {
productAdminService.productTypes.find {it.id == productTypeId}
}
Collection<AttributeTypeCommand> getAttributeTypes() {
attributeTypeIds.collect {id ->
productAdminService.getAttributeType(id)
}
}
void setAttributeTypes(Collection<AttributeTypeCommand> attributeTypes) {
this.attributeTypeIds = attributeTypes.collect {it.id}
}
}
实际发生的是, code> attributeTypeIds 和 productTypeId
属性绑定到相关的请求参数,getters / setterssimulate productType
和 attributeTypes
属性。有一个更简单的方法来填充命令对象的关联?
What actually happens is that the attributeTypeIds
and productTypeId
properties are bound to the relevant request parameters and the getters/setters "simulate" productType
and attributeTypes
properties. Is there a simpler way to populate the associations of a command object?
推荐答案
您是否真的需要为attributeTypes和productType属性设置子命令?任何你不使用PropertyEditorSupport绑定的原因?例如:
Do you actually need to have sub-commands for attributeTypes and productType properties? Any reason you're not using PropertyEditorSupport binding? E.g.:
public class ProductTypeEditor extends PropertyEditorSupport
{
ProductAdminService productAdminService // inject somewhow
void setAsText(String s)
{
if (s) value = productAdminService.productTypes.find { it.id == s.toLong() }
}
public String getAsText()
{
value?.id
}
}
(和attributeType对象类似),并在编辑器注册商中注册:
(and something similar for attributeType object), and register these in a editor registrar:
import java.beans.PropertyEditorSupport
public class CustomEditorRegistrar implements PropertyEditorRegistrar {
public void registerCustomEditors(PropertyEditorRegistry reg) {
reg.registerCustomEditor(ProductType, new ProductTypeEditor())
reg.registerCustomEditor(AttributeType, new AttributeTypeEditor())
}
}
资源oovy:
beans =
{
customEditorRegistrar(CustomEditorRegistrar)
}
然后在您刚刚拥有的Cmd中:
then in your Cmd you just have:
class ProductCommand {
String name
List<AttributeType> attributeTypes = []
ProductType productType
}
如果您 需要实际的子命令关联,那么我已经做了类似于@Andre Steingress的建议,结合PropertyEditorSupport绑定:
If you do need actual sub-command associations then I've done something similar to what @Andre Steingress has suggested, in combination with PropertyEditorSupport binding:
// parent cmd
import org.apache.commons.collections.ListUtils
import org.apache.commons.collections.FactoryUtils
public class DefineItemConstraintsCmd implements Serializable
{
List allItemConstraints = ListUtils.lazyList([], FactoryUtils.instantiateFactory(ItemConstraintsCmd))
//...
}
// sub cmd
@Validateable
class ItemConstraintsCmd implements Serializable
{
Item item // this has an ItemEditor for binding
//...
}
希望我没有误解你想要实现的目标:)
Hopefully I've not misunderstood what you're trying to achieve :)
这篇关于Grails命令对象数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!