Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4547404
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T16:05:17+00:00 2026-05-21T16:05:17+00:00

Grails has very good support for binding request parameters to a domain object and

  • 0

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
}

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

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

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T16:05:18+00:00Added an answer on May 21, 2026 at 4:05 pm

    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<AttributeType> 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 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Grails vs Rails. Which has better support? And which one is a better choice
Hy, I'm developing a Grails app which has to communicate with an existing Java
I'm a pretty experienced Grails developer, but most of my experience has been with
Grails makes it very easy to configure datasources for different environments (development, test, production)
In a Grails domain class, how do I set the constraint for a String
I am developing a grails application which has codenarc plugin. And also I am
I have developed a grails/groovy application for a legacy database which has user maintenance
I have integrated spring security plugin with my grails application, which has hibernate as
I have a Grails domain class that is a hierarchy of categories. Each Category
This question has some code to add to the controller closure, which is fine,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.