Announcement Announcement Module
Collapse
No announcement yet.
Binding a multiple select to a Collection? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Binding a multiple select to a Collection?

    How do I bind a multi-select box to a list of custom objects?

    I have a multi-select, like this:

    Code:
    <form:select path="countries" multiple="true" size="3">
    	<c:forEach items="${countryList}" var="country">
    		<form:option value="${country.abbreviation}">
    			<spring:message code="${country.key}" />
    		</form:option>
    	</c:forEach>
    </form:select>
    And my backing object/command has a field with getters/setters like this:

    Code:
    private List<Country> countries;
    
    public List<Country> getCountries() {
    	return countries;
    }
    
    public void setCountries( List<Country> countries ) {
    	this.countries = countries;
    }
    I've created a custom editor (extends PropertyEditorSupport) that will convert a request parameter String into a Country object, but the "List" part of the binding is not working because the entire string is being passed to my custom Country editor.

    For example, if the countries "United States" and "Canada" are selected, then the request parameter will be "US, CA". Instead of the custom Country editor getting called twice with "US" being passed in first and "CA" second, the entire string ("US, CA") is passed into the custom Country editor.

    I'm starting to look into the CustomCollectionEditor class, but so far have not found any solution. Please help me out as this is really important to my application.

    Thanks,
    Eric

  • #2
    Nevermind, I was using a mock request in a unit test and didn't realize that multiple select were stored in a String[] not a String.

    Comment


    • #3
      I have similar problem, i need to bind multiple select values to set object. Anybody can help?

      Comment

      Working...
      X