在Struts2中从URL填充Action类实例变量

时间:2015-10-07 03:44:56

标签: java struts2 ognl

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

testBean这个

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

TestAction

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

当我的网址为http://localhost:8080/Struts2Test/test?testBeanHolder.testBeanMap[0].value=1时,我的操作的testBeanHolder.testBeanMap会填充0映射到TestBean value=1实例的密钥。

当网址为http://localhost:8080/Struts2Test/test?testBeanHolder.testBeanList[0].value=1时,testBeanHolder.testBeanList会使用TestBean的{​​{1}}单个实例进行填充。

我尝试填充value=1的{​​{1}}属性并且无效。 testBeanListMap已创建但为空。这是我正在尝试的网址testBeanHolder

3 个答案:

答案 0 :(得分:1)

这是有效的代码,添加了修改后的类:

TestBeanListHolder

package com.struts2test.beans;

import java.util.List;

public class TestBeanListHolder {
    private List<TestBean> testBeans;

    public List<TestBean> getTestBeans() {
        return testBeans;
    }

    public void setTestBeans(List<TestBean> testBeans) {
        this.testBeans = testBeans;
    }

}

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, TestBeanListHolder> testBeanListMap;

    public Map<Integer, TestBean> getTestBeanMap() {
        return testBeanMap;
    }

    public void setTestBeanMap(Map<Integer, TestBean> testBeanMap) {
        this.testBeanMap = testBeanMap;
    }

    public Map<Integer, TestBeanListHolder> getTestBeanListMap() {
        return testBeanListMap;
    }

    public void setTestBeanListMap(
            Map<Integer, TestBeanListHolder> testBeanListMap) {
        this.testBeanListMap = testBeanListMap;
    }

    public List<TestBean> getTestBeanList() {
        return testBeanList;
    }

    public void setTestBeanList(List<TestBean> testBeanList) {
        this.testBeanList = testBeanList;
    }
}

URL

http://localhost:8080/Struts2Test/test?testBeanHolder.testBeanListMap[1].testBeans[0].value=somevalue

答案 1 :(得分:0)

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

答案 2 :(得分:-1)

表达式testBeanHolder.testBeanListMap[0][0].value不是有效的OGNL表达式。

有关允许的内容的完整参考,请参阅here