Custom data binding for collecting abstract classes in a command object

In the Grails 3.x project, I have a command object HabitatCommandthat has a parameterized collection of animals:

class HabitatCommand {
    Set<Animal> animals = []
}

Animal is an abstract class, and there are many specific classes of animals:

abstract class Animal {
    String name
    Integer height
    Integer weight
}

class Bird extends Animal {
    Integer wingspan
}

class Cat extends Animal {
    Integer lives
}

I’m sure that data binding works fine if we just select from existing animals and send their identifiers (as well as from the drop-down list), but there are problems when trying to create new animal instances in the form. Take the following form data:

animals[0].name: 'animal1'
animals[0].height: 4
animals[0].weight: 75
animals[0].wingspan: 7
animals[1].name: 'animal2'
animals[1].height: 3
animals[1].weight: 45
animals[1].lives: 9

, Grails animals[i] , Animal. , Animal , Animal , , wingspan, Bird.

, , , ?

? [].withDefault {}, @BindUsing ValueConverter, , , .

+4

All Articles