Announcement Announcement Module
Collapse
No announcement yet.
Binding a collection that contains interfaces Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Binding a collection that contains interfaces

    Hi there everyone,

    I'm using Spring 2.5.5.

    My problem is binding a list that doesn't hold items of a concrete class, but items of a certain interface, that can dynamically be of different concrete classes. I can't use the LazyList solution with this model, since the LazyList expects to hold items of a very concrete type.

    This is the model:

    Code:
    public class Definition {
    	private List<Field> fieldSelections = LazyList.decorate(
    			new ArrayList<Field>(),
    			FactoryUtils.instantiateFactory(Field.class));
    }
    Field is an interface.
    There are two subclasses, FieldA and FieldB, for that matter:

    Code:
    public interface Field {
      // No implementation needed.
    }
    
    public class FieldA implements Field {
      private String foo;
    }
    
    public class FieldB implements Field {
      private int bar;
    }
    Each subclass has members (with getters/setters of course) that might be different.

    In the view, I go over the fieldSelections list, check each item's type, and display it slightly differently since each item has different members according to its type:

    Code:
    <c:forEach items="${MyModel.definition.fieldSelections}" var="curField" varStatus="curFieldStatus">
      <c:if test="${curField.class.name == 'some.package.FieldA'}">
        <form:input path="MyModel.definition.fieldSelections[${curFieldStatus.index}].foo" />
      </c:if>
      <c:if test="${curField.class.name == 'some.package.FieldB'}">
        <form:input path="MyModel.definition.fieldSelections[${curFieldStatus.index}].bar" />
      </c:if>
    </c:forEach>
    I begin to think that perhaps the fact that the fieldSelections list doesn't hold a concrete type, and that each subclass has different members, is problematic conceptually - how could Spring bind the 'foo' and 'bar' fields to an item in the list, without ever knowing that these fields exist in the item...?

    The exception, btw, is as follows:

    Code:
    java.lang.IllegalArgumentException: InstantiateFactory: The constructor must exist and be public
    That makes sense, for a FactoryUtils can't instantiate an interface.

    Is there a way to wire everything here?

    One way would be changing my model. I can hold two different lists, for example - one for FieldA and another for FieldB, this way using the LazyList solution effectively. Yet this means my view will be different, and I don't really want to change my view just because I have binding problems...

    Another way I thought of is registering a binder for the classes, or interfaces, or something, but I don't quite know how to wire it up.

    Many thanks in advance,
    Daniel

  • #2
    Hi Daniel,

    I'm having a similar problem, did you ever get this working ?

    Comment


    • #3
      Hi Daniel,

      did you get this working.

      Comment

      Working...
      X