java Map 中的 Spring 绑定值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4511647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 06:41:14  来源:igfitidea点击:

Spring binding values in a Map

javaspringbindingspring-mvc

提问by stevebot

Is there a way to Spring bind values in a map?

有没有办法在地图中绑定值?

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<String,String>,我想在其中绑定特定的值。用户将在 input 元素中键入一些内容,该 input 元素的值将绑定到与映射中特定键关联的值。

回答by axtavt

Yes, you can do it with [...]syntax. The Mapitself, however, should be a property of the command object:

是的,您可以使用[...]语法来做到这一点。的Map本身,但是,应该是命令对象的属性:

public class Form {
    private Map<String, String> values = ...;
    ...
}

Then you submit a form with the input field named values['foo'], i.e. with Spring form tags it would be a path:

然后您提交一个输入字段名为 的values['foo']表单,即使用 Spring 表单标签,它将是一个path

<form:input path = "values['foo']" />

or namein plain HTML:

name在纯 HTML 中:

<input name = "values['foo']" type = "text" />