Java: Spring Framework: Declaring Nested Maps

2019-03-20 20:02发布

问题:

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.

  <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?

回答1:

For complex value types, do not nest the map element, instead use value-ref attributes. By default, value elements only accept String values.

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.

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>


回答2:

<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>


标签: java maps spring