John S
John S

Reputation: 21482

Binding multiple select where option values may contain commas in Spring 3

We are having an issue with binding a multiple select element when the option values contain commas. We have tried binding to both a String and to a List<String>, but have issues with both.

When a multiple select element is posted, the value of each selected option is passed in a separate request parameter, all with the same name. For example, if the select element name is "code", the parameters might look like this:

code=ABC
code=A,B
code=XYZ

When binding to a String, Spring will automatically join these values into a comma-separated string. That is obviously an issue if one or more of the values contains a comma.

When binding to a List<String>, things work fine when multiple options are selected. In that case, Spring creates a List with an entry for each selected option. But if only one option is selected, Spring assumes the value is a comma-separated list and will split it into multiple entries.

Is there a way to tell Spring to use a different character than a comma when binding to a String? Is there a way to tell Spring not to split a single value when binding to a List<String>? Or is there another way to deal with this?

Upvotes: 2

Views: 2140

Answers (1)

vincentks
vincentks

Reputation: 694

I believe this thread is related to your issue: How to prevent parameter binding from interpreting commas in Spring 3.0.5?. This Spring issue may also be helpful: https://jira.springsource.org/browse/SPR-7963

The solution provided at https://stackoverflow.com/a/5239841/1259928, which details how to create a new conversion service which uses a different string separator and wiring it into Spring config should do the trick.

Upvotes: 4

Related Questions