Struts2发布List <map <string,t>&gt;回到行动</map <string,t>

时间:2012-06-01 16:40:41

标签: java jsp struts2 ognl xwork

我正在使用struts 2.1.8和ognl 2.7.3。

在我的动作课上,我有一个字段(地图的键:'foo','bar'):

public class TestAction extends ActionSupport {

private static Log log = LogFactory.getLog(TestAction.class);

private static final long serialVersionUID = -4684320206927996693L;

private List<Map<String, Long>> myData = new ArrayList<Map<String,Long>>();

public TestAction() {
    super();
}

public String execute() {

    Map<String,Long> obj1 = new HashMap<String,Long>();
    obj1.put("foo", 111L);
    obj1.put("bar", 112L);

    Map<String,Long> obj2 = new HashMap<String,Long>();
    obj2.put("foo", 551L);
    obj2.put("bar", 552L);

    myData.add(obj1);
    myData.add(obj2);

    return INPUT;
}

public String save() {
    log.info("In save()");

    log.info("--> " + CollectionUtil.dump(myData));

            return SUCCESS;
}

public List<Map<String, Long>> getMyData() {
    return myData;
}

public void setMyData(List<Map<String, Long>> myData) {
    this.myData = myData;
}
}

我可以在JSP页面上显示内容,但是当我尝试提交回动作时出现转换错误。

JSP代码是:

<s:form action="Test_save">

    <s:if test="myData != null && myData.size > 0">
          <s:iterator value="myData" status="status">
              <tr>
                  <td>
                      <s:textfield name="myData[%{#status.index}].foo" />
                  </td>
                  <td>
                      <s:textfield name="myData[%{#status.index}].bar" />
                  </td>
              </tr>
          </s:iterator>
    </s:if>

    <s:submit value="Submit"></s:submit>
</s:form>

以下是日志文件中输出的内容:

[75913] DEBUG  com.opensymphony.xwork2.interceptor.StaticParametersInterceptor debug  -> Setting static parameters {}
[75913] DEBUG  com.opensymphony.xwork2.interceptor.ParametersInterceptor debug  -> Setting params myData[0].bar => [ 112 ] myData[0].foo => [ 111 ] myData[1].bar => [ 552 ] myData[1].foo => [ 551 ] 
[75917] DEBUG  com.opensymphony.xwork2.conversion.impl.XWorkConverter debug  -> Property: CreateIfNull_myData
[75918] DEBUG  com.opensymphony.xwork2.conversion.impl.XWorkConverter debug  -> Class: com.mcw.web.actions.TestAction
[75923] WARN   com.opensymphony.xwork2.ognl.OgnlValueStack   warn  -> Error setting value
java.util.Map - Class: java.lang.ClassFile: Class.javaMethod: newInstance0 Line: 340 - java/lang/Class.java:340:-1
at com.opensymphony.xwork2.ognl.accessor.XWorkListPropertyAccessor.getProperty(XWorkListPropertyAccessor.java:102)
    at ognl.OgnlRuntime.getProperty(OgnlRuntime.java:2210)
    at ognl.ASTProperty.getValueBody(ASTProperty.java:114)
    at ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)
    at ognl.SimpleNode.getValue(SimpleNode.java:258)
    at ognl.ASTChain.setValueBody(ASTChain.java:222)
    at ognl.SimpleNode.evaluateSetValueBody(SimpleNode.java:220)
    at ognl.SimpleNode.setValue(SimpleNode.java:301)
    at ognl.Ognl.setValue(Ognl.java:737)
    ....
    Caused by: java.lang.InstantiationException: java.util.Map
    at java.lang.Class.newInstance0(Class.java:340)
    at java.lang.Class.newInstance(Class.java:308)
    at com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:119)
    at com.opensymphony.xwork2.ognl.accessor.XWorkListPropertyAccessor.getProperty(XWorkListPropertyAccessor.java:100)
    ... 82 more

我错过了什么?

1 个答案:

答案 0 :(得分:0)

我认为你的提交是错误的,因为没有值会回到提交操作,你只是在你的浏览器中显示数据,但没有数据提交到test_save动作,你应该添加一些输入,它们的值是显示数据