Grails command object data binding

前端 未结 4 612
北海茫月
北海茫月 2020-12-05 00:50

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

4条回答
  •  北海茫月
    2020-12-05 01:04

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

    (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())
        }
    }
    

    And register in your resources.groovy:

    beans =
    {
        customEditorRegistrar(CustomEditorRegistrar)
    }
    

    then in your Cmd you just have:

    class ProductCommand {
        String name
        List attributeTypes = []
        ProductType productType
    }
    

    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 :)

提交回复
热议问题