2

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?

John S
  • 21,212
  • 8
  • 46
  • 56
  • 1
    I believe this thread is related to your issue: http://stackoverflow.com/questions/4998748/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 – vincentks Jun 11 '13 at 19:17
  • @vincentks - Thank you for those links. They are definitely related. I always included "multiple select" in my searches. That's probably why I didn't find those. – John S Jun 11 '13 at 19:50
  • @vincentks Why you didn't put that into an actual answer? – Pavel Horal Jun 12 '13 at 21:15

1 Answers1

4

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.

Community
  • 1
  • 1
vincentks
  • 694
  • 6
  • 14