Struts2 post List<Map<String,T>> back to an action

2.3k views Asked by At

I am using struts 2.1.8 and ognl 2.7.3.

On my action class, I have a field (the map's keys: '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;
}
}

I can display the contents on the JSP page, but I get conversion errors when I try to submit back to the action.

The JSP code is:

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

Here is what is output in the log file:

[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

What am I missing?

1

There are 1 answers

1
heartneofully On

I think your submit is wrong, beacause no value will back to the submit action, you just show the data in your browser,but no data to submit to the test_save action,you should add some inputs and their values are the show data.