Java:Spring 框架:声明嵌套映射
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/963599/
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
Java: Spring Framework: Declaring Nested Maps
提问by Chadwick
I get an error at 4th line saying: cvc-complex-type.2.4.d: Invalid content was found starting with element 'map'. No child element is expected at this point.
我在第 4 行收到一条错误消息:cvc-complex-type.2.4.d:发现以元素“map”开头的无效内容。此时不需要子元素。
<util:map id="entirePayTypesMap">
<entry key="34">
<value>
<map>
<entry key="default">
<value>
<map key-type="java.lang.Boolean">
<entry key="true" value="3T" />
<entry key="false" value="3U" />
</map>
</value>
</entry>
</map>
</value>
</entry>
</util:map>
Any suggestions?
有什么建议?
回答by Chadwick
For complex value types, do not nest the map element, instead use value-refattributes. By default, valueelements only accept Stringvalues.
对于复杂的值类型,不要嵌套 map 元素,而是使用value-ref属性。默认情况下,value元素只接受String值。
The property may be a string, or may be converted to the required type using the JavaBeans PropertyEditor machinery. This makes it possible for application developers to write custom PropertyEditor implementations that can convert strings to arbitrary target objects.
Note that this is recommended for simple objects only. Configure more complex objects by populating JavaBean properties with references to other beans.
该属性可以是字符串,也可以使用 JavaBeans PropertyEditor 机制转换为所需的类型。这使得应用程序开发人员可以编写自定义的 PropertyEditor 实现,将字符串转换为任意目标对象。
请注意,这仅建议用于简单对象。通过使用对其他 bean 的引用填充 JavaBean 属性来配置更复杂的对象。
Your data will look something like:
您的数据将类似于:
<util:map id="mapA" key-type="java.lang.Boolean">
<entry key="true" value="3T" />
<entry key="false" value="3U" />
</util:map>
<util:map id="map1">
<entry key="default" value-ref="mapA"/>
</util:map>
<util:map id="mapB" key-type="java.lang.Boolean">
<entry key="true" value="4T" />
<entry key="false" value="4U" />
</util:map>
<util:map id="map2">
<entry key="default" value-ref="mapB"/>
</util:map>
<util:map id="entirePayTypesMap">
<entry key="34" value-ref="map1"/>
<entry key="35" value-ref="map2"/>
</util:map>
回答by setzamora
<util:map id="map1" map-class="java.util.HashMap" key-type="java.lang.String" value-type="java.util.HashMap">
<entry key="" value-ref="map2">
</util:map>
<util:map id="map2" map-class="java.util.HashMap" key-type="java.lang.String" value-type="java.util.HashMap">
<entry key="" value-ref="map3">
</util:map>
<util:map id="map3" map-class="java.util.HashMap" key-type="java.lang.String" value-type="java.lang.Boolean">
<entry key="" value="">
</util:map>

