Announcement Announcement Module
No announcement yet.
Is it possible to bind to a java.util.Set? (SpringMVC) Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Is it possible to bind to a java.util.Set? (SpringMVC)

    There are some cases in my application where my model would be much better modeled as a Set then a List (unique guaruntee). I've noticed that SpringMVC uses the List get(int) method. Since there is no corresponding method on Set, it would appear that it is not possible to bind to it. Is this correct?

  • #2
    Set binding

    You will need a set with predictably ordered iteration implying such a set as LinkedHashSet. With this satisified you would provide an accessor (against which data binding occurrs) for your set returning an indexable collection:

      public AccountAddress[] getAccountAddresses() {
        if(addresses == null) return null;
        return (AccountAddress[]) addresses.toArray(new AccountAddress[addresses.size()]);
    where addresses is a java.util.LinkedHashSet


    • #3
      I did an implementation for it, but I am not sure it is a good thing. It works fine though, I would probably not use it for big sets.
      I implement a custom editor which does:
      - getAsText returns a ; list of ids for my objects (and display as well)
      - setAsText retrieves the list, populates a new set and uses setValue()
      The web allows to add remove items and basically maintains the string of ; separated values

      I am not sure I would use that for big sets but works well enough.


      • #4
        Spring will natively bind to an Array, but I am not sure about other collection types. It is something I keep meaning to play with. See