将值从我的html表单传递到操作类时遇到问题。创建了一个示例项目来测试功能,并在此处遇到相同的问题。我有以下课程:

测试豆

package com.struts2test.beans;

public class TestBean {
    private String value;

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}


TestBeanHolder

package com.struts2test.beans;

import java.util.List;
import java.util.Map;

public class TestBeanHolder {
    private Map<Integer, TestBean> testBeanMap;
    private List<TestBean> testBeanList;
    private Map<Integer, List<TestBean>> testBeanListMap;

    public Map<Integer, TestBean> getTestBeanMap() {
        return testBeanMap;
    }
    public void setTestBeanMap(Map<Integer, TestBean> testBeanMap) {
        this.testBeanMap = testBeanMap;
    }
    public Map<Integer, List<TestBean>> getTestBeanListMap() {
        return testBeanListMap;
    }
    public void setTestBeanListMap(Map<Integer, List<TestBean>> testBeanListMap) {
        this.testBeanListMap = testBeanListMap;
    }
    public List<TestBean> getTestBeanList() {
        return testBeanList;
    }
    public void setTestBeanList(List<TestBean> testBeanList) {
        this.testBeanList = testBeanList;
    }
}


测试动作

package com.struts2test.action;

import com.opensymphony.xwork2.ActionSupport;
import com.struts2test.beans.TestBeanHolder;

public class TestAction extends ActionSupport {

    private static final long serialVersionUID = 1L;
    private TestBeanHolder testBeanHolder;

    public TestBeanHolder getTestBeanHolder() {
        return testBeanHolder;
    }

    public void setTestBeanHolder(TestBeanHolder testBeanHolder) {
        this.testBeanHolder = testBeanHolder;
    }

    public String execute() throws Exception {
        return SUCCESS;
    }
}


当我的URL是http://localhost:8080/Struts2Test/test?testBeanHolder.testBeanMap[0].value=1时,我的操作的testBeanHolder.testBeanMap将填充0的键,该键映射到TestBeanvalue=1实例。

当url为http://localhost:8080/Struts2Test/test?testBeanHolder.testBeanList[0].value=1时,将使用testBeanHolder.testBeanListTestBean单个实例填充value=1

我尝试填充testBeanListMaptestBeanHolder属性,但不起作用。 testBeanListMap已创建但为空。这是我正在尝试的网址http://localhost:8080/Struts2Test/test?testBeanHolder.testBeanListMap[0][0].value=1

最佳答案

网址http://localhost:8080/Struts2Test/test?testBeanHolder.testBeanListMap[0][0].value=1无法使用,因为您使用了错误的参数名称。因此,testBeanHolder.testBeanListMap[0][0].value是映射到对象的参数名称,该对象具有复杂类型的属性(集合的集合)。 Struts2无法处理这种情况。但是您可以使用对象包装第二个集合并使用对象的集合。名称将更改为testBeanHolder.testBeanListMap[0].object[0].value

10-04 11:59