Grails command object data binding

前端 未结 4 610
北海茫月
北海茫月 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条回答
  •  -上瘾入骨i
    2020-12-05 01:19

    What I've seen in some projects was the use of the Lazy* collection classes from Apache Commons Collections. It used code like this to lazily initialize a command association:

    class ProductCommand {
    
      String name
      String type
    
      List attributes = org.apache.commons.collections.list.LazyList.decorate(new ArrayList(), new org.apache.commons.collections.functors.InstantiateFactory(AttributeTypeCommand.class))
    }
    
    class AttributeTypeCommand {
      // ...
    }
    

    With the example given above, the GSP could reference association indices

    This works even for non-existent indices since every get(index) call on LazyList evaluates whether the list already has an element on that position and if not, the list will automatically grow in size and return a new object from the specified factory.

    Note that you could also use LazyMap in order to create the similar code with lazy maps:

    http://commons.apache.org/collections/apidocs/org/apache/commons/collections/map/LazyMap.html

    http://commons.apache.org/collections/apidocs/org/apache/commons/collections/list/LazyList.html

    Update:

    Groovy 2.0 (which is not yet part of the Grails distribution) will come with embedded support for lazy and eager lists. I wrote a blog post on this topic:

    http://blog.andresteingress.com/2012/06/29/groovy-2-0-love-for-grails-command-objects/

    Update:

    With the release of Grails 2.2.0, Groovy 2.0 is part of the distribution.

    http://blog.andresteingress.com/2012/06/29/groovy-2-0-love-for-grails-command-objects/

提交回复
热议问题