面对将值从html表单传递到action类的问题。创建了一个示例项目来测试功能,并在此处遇到相同的问题。我有以下课程:
package com.struts2test.beans;
public class TestBean {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
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;
}
}
当我的网址为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
答案 0 :(得分:1)
这是有效的代码,添加了修改后的类:
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;
}
}
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;
}
}
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。