Falcon
Falcon

Reputation: 3160

Bind comma-separated string to List

I have a form with a text-input element.

The user can enter values separated by commas like "a,b,c,d". In my model, there's a List which should then hold these values (which really are a list).

In Thymeleaf, how can I bind such a string and convert it to a list on submit? Is there something like a Converter interface?

Upvotes: 2

Views: 2263

Answers (1)

Rana_S
Rana_S

Reputation: 1550

In your Model instead of List you can use String to populate the input values. After you get the values, you can do following:

You will probably have a String something like this: "text1, text2, text3...". Then convert it to a List you can do something like this:

String value = "text1, text2, text3..."
List<String> inputs = Arrays.asList(value.split("\\s*,\\s*"));

The regex basically removes the whitespace and the comma. This should work fine.

Upvotes: 1

Related Questions