Struts 2 - 在地图内的地图中保存值

时间:2013-05-24 21:02:06

标签: java loops dictionary struts2 type-conversion

我目前正在使用Struts 2,我遇到了这种情况:

在我的Action类中:

private Map<String, Map<Integer, MyEntity>> map; 

public void setMap(Map<String, Map<Integer, MyEntity>> map) { 
      this.map = map; 
} 

public Map<String, Map<Integer, MyEntity>> getMap() { 
      return this.map; 
} 

在我的JSP中,我使用迭代器来使用&#39;键&#39;变量,然后遍历另一个(整数)列表并使用此整数来遍历第二个映射,如下所示:map[${key}][${myInteger}]

<s:iterator value="map" status="status"> 
  <tr> 
    <td>${status.index}</td> 
    <td>${key}</td> 
    <s:iterator value="integersList" id="myInteger"> 
      <td> 
        <s:textfield name="map[${key}][${myInteger}].myEntityAttribute" value="%{map[#attr.key][#attr.myInteger].myEntityAttribute}" id="attr_${key}_${myInteger}" theme="simple" size="10" /> 
      </td> 
    </s:iterator> 
  </tr> 
</s:iterator>

这样做可以使值正常工作:value="%{map[#attr.key][#attr.myInteger].myEntityAttribute}"

但是当我想保存更改时,我无法使其正常工作:name="map[${key}][${myInteger}].myEntityAttribute"

我不知道我做错了什么,或者如果不能做到这一点(我很确定必须有办法),但我有点厌倦了尝试。

我很感激任何建议,真的,任何有用的东西都会受到欢迎。

1 个答案:

答案 0 :(得分:0)

很快,使用Struts默认类型转换器(XWorkConverter)是不可能的。它正在寻找bean属性来执行转换。

也支持集合转换,还有一个CollectionConverter,您可以使用它来播放像您这样的集合。

为此,您应该为xwork-conversion.properties中的相应集合类配置转换器。它是struts转换器使用的转换的全局属性。如果您不这样做,Struts转换器会使您的字段出现ognl.ConvertionNotSupported异常。您可以通过为您转换的类型添加自己的类型转换器来解除它。