Map 中的 Spring 绑定值 [英] Spring binding values in a Map
问题描述
有没有办法在地图中绑定值?
Is there a way to Spring bind values in a map?
例如,我有一个 Map
并且我想在其中绑定特定的值.用户将在 input 元素中键入一些内容,该 input 元素的值将绑定到与映射中特定键关联的值.
For instance, I have a Map<String,String>
and I want to spring bind specific values in it.
The user will type something into a input element, and the value of that input element will get bound to the value associated with a specific key in the map.
推荐答案
是的,您可以使用 [...]
语法来完成.然而,Map
本身应该是命令对象的一个属性:
Yes, you can do it with [...]
syntax. The Map
itself, however, should be a property of the command object:
public class Form {
private Map<String, String> values = ...;
...
}
然后您提交一个表单,输入字段名为 values['foo']
,即使用 Spring 表单标签,它将是一个 path
:
Then you submit a form with the input field named values['foo']
, i.e. with Spring form tags it would be a path
:
<form:input path = "values['foo']" />
纯 HTML 中的
或 name
:
or name
in plain HTML:
<input name = "values['foo']" type = "text" />
这篇关于Map 中的 Spring 绑定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!