Spring binding values in a Map

混江龙づ霸主 提交于 2019-12-18 15:27:21

问题


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.


回答1:


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 = ...;
    ...
}

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']" />

or name in plain HTML:

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


来源:https://stackoverflow.com/questions/4511647/spring-binding-values-in-a-map

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!