开发者

Unwanted comma-splitting when binding string (from list box) to list in Spring

开发者 https://www.devze.com 2023-02-28 21:30 出处:网络
I have a standard multi-select list box bound to a List property of an object. The problem is that when a single value in the list box is selected, and that value contains a comma, it\'s being split

I have a standard multi-select list box bound to a List property of an object.

The problem is that when a single value in the list box is selected, and that value contains a comma, it's being split into a list of two items when the incoming data is bound to the list property.

eg. if the list box item is "I contain,a comma", the property is set to a list containing two elements: "I contain" and "a comma".

I'm using Spring 3.0.5 and the mvc:annotation-driven, so am getting the standard converters as set up by FormattingConversionServiceFactoryBean; somewhere in there, StringToCollectionConverter is being called. While this must be useful elsewhere (in Spring's internals) I don't want it here.

Anyone know the correct way to get around this? It's such an obvious and simple problem I can't help but think I'm missing so开发者_Go百科mething obvious here; can it really be a bug/oversight in Spring? No doubt there will be various ways of configuring Converters or PropertyEditors to work around this, but there must be an elegant and framework-friendly answer, surely?

Form tag for good measure:

<form:select path="someListProperty" multiple="true" items="${possibleValuesForSomeListProperty}" size="5" itemLabel="name" itemValue="name" />

Cheers.


What seems to be the obvious answer to me is to keep the list on the server side and let the client side select only the offsets, not the actual values:

<select name="list" >
<option value="0">Foo</option>
<option value="1">Bar</option>
<option value="2">Phleem</option>
</select>

Now in your controller, construct the List and activate the items whose index is selected. That way you solve your problem and also give malicious clients less opportunities to manipulate the request values.

0

精彩评论

暂无评论...
验证码 换一张
取 消