Announcement Announcement Module
No announcement yet.
form:select - working, can spring convert to objects based on id? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • form:select - working, can spring convert to objects based on id?

    I have form:select working with returning string id's, and I can take those strings id's and iterate through the original collection and get the original objects - but isn't there some way to get spring mvc to do this automatically?


    Longer Version

    I've searched and searched for the answer to this, but to no avail so far (other than "no, it can't").

    I have a working multi-select box -

    <form:select path="locationsSelectedString" multiple="true">
    <form:options items="${contact.locationsAvailable}" itemValue="id" itemLabel="label"/>

    If I set the path variable to a String value, I get back a list of id's - for example "location1,location2".
    If I set the path variable to a String[] value, I get back an array with the id's - "location1" and "location2"
    If I set the path variable to a List<String> value, I get back a List with the id's - "location1" and "location2"

    Whatever I used, I can take that list of id's, iterate through the contact.locationsAvailable list -
    private List<Location> locationsAvailable;

    And get back the original objects.

    My question is - isn't there some way to get Spring MVC to do this for me? It seems like all the info is there, and it's a really common case, it seems like there must be some way to get Spring to do this automatically without constantly rewriting the same it out there somewhere?

  • #2
    I strongly suggest a read of the reference guide. Spring can perfectly do it but your approach is just wrong... You should bind to a collection of Location objects (selectedLocations maybe?) and let Spring convert your id to a Location. You need a customized CollectionEditor for this.

    As stated checkout the reference guide especially the part on data binding and type conversion.


    • #3
      Hey, thanks for an answer.

      Sound like it doesn't have any easy way to do this automatically. I looked through the reference guide like you suggested -

      Spent 15 minutes trying to figure out what they're talking about, and - didn't make any progress in figuring out how this would apply to what I'm trying to do. As you mentioned I would need a customized CollectionEditor to do it anyways, sounds like there's a good chance I would just end up pouring a bunch of time into figuring it out - only to need to write the same code myself over and over again for each kind of collection, just in the CollectionEditor rather than directly in the method, which would make things more difficult.
      Last edited by PaulRivers; Jun 6th, 2012, 04:40 PM.


      • #4
        You could create a CustomCollectionEditor which takes a type and uses an entitymanager to load the correct instances, that way you have to write the code only once instead over and over. (This is assuming you use JPA the same would work for Hibernate).

        You don't want that code in your method as you want to simply work with your objects, you don't want to work with a comma seperated string, cut it in pieces yourself etc. you want your framework to handle that for you.