在 Spring 中将字符串(来自列表框)绑定到列表时出现不需要的逗号分割
我有一个绑定到对象的 List 属性的标准多选列表框。
问题是,当选择列表框中的单个值并且该值包含逗号时,当传入数据绑定到列表属性时,它会被拆分为两个项目的列表。
例如。如果列表框项目是“我包含,逗号”,则该属性设置为包含两个元素的列表:“我包含”和“逗号”。
我正在使用 Spring 3.0.5 和 mvc:annotation-driven,所以我得到了 FormattingConversionServiceFactoryBean 设置的标准转换器;在那里的某个地方,正在调用 StringToCollectionConverter 。虽然这在其他地方(在 Spring 的内部)一定有用,但我不希望在这里使用它。
有人知道解决这个问题的正确方法吗?这是一个如此明显和简单的问题,我情不自禁地认为我在这里遗漏了一些明显的东西;这真的是 Spring 的一个错误/疏忽吗?毫无疑问,会有多种配置转换器或属性编辑器的方法来解决这个问题,但是肯定有一个优雅且框架友好的答案吗?
良好措施的表单标签:
<form:select path="someListProperty" multiple="true" items="${possibleValuesForSomeListProperty}" size="5" itemLabel="name" itemValue="name" />
干杯。
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 something 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对我来说,显而易见的答案是将列表保留在服务器端,并让客户端仅选择偏移量,而不是实际值:
现在在您的控制器中,构造列表并激活选择索引的项目。这样您就可以解决问题,同时也减少恶意客户端操纵请求值的机会。
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:
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.