在 Struts2 中填充来自 URL 的 Action class 实例变量
Populating Action class instance variables from URL in Struts2
在将值从我的 html 表单传递到操作 class 时遇到问题。创建了一个示例项目来测试功能并在此处遇到相同的问题。我有以下 classes:
测试对象
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
映射到 TestBean
实例 [=20] =].
当 url 为 http://localhost:8080/Struts2Test/test?testBeanHolder.testBeanList[0].value=1
时,testBeanHolder.testBeanList
会填充 TestBean
和 value=1
的单个实例。
我尝试填充 testBeanListMap
属性 的 testBeanHolder
,但没有用。 testBeanListMap
已创建但为空。这是 URL 我正在尝试 http://localhost:8080/Struts2Test/test?testBeanHolder.testBeanListMap[0][0].value=1
表达式 testBeanHolder.testBeanListMap[0][0].value
不是有效的 OGNL 表达式。
有关允许的内容的完整参考,请参阅 here。
url http://localhost:8080/Struts2Test/test?testBeanHolder.testBeanListMap[0][0].value=1
将不起作用,因为您使用了错误的参数名称。因此 testBeanHolder.testBeanListMap[0][0].value
是映射到对象的参数名称,该对象具有复杂类型(集合的集合)的 属性。 Struts2 无法处理这种情况,。但是您可以用一个对象包装第二个集合并使用对象集合。名称将更改为 testBeanHolder.testBeanListMap[0].object[0].value
.
这是有效的代码,添加修改后的 类:
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
在将值从我的 html 表单传递到操作 class 时遇到问题。创建了一个示例项目来测试功能并在此处遇到相同的问题。我有以下 classes:
测试对象
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
映射到 TestBean
实例 [=20] =].
当 url 为 http://localhost:8080/Struts2Test/test?testBeanHolder.testBeanList[0].value=1
时,testBeanHolder.testBeanList
会填充 TestBean
和 value=1
的单个实例。
我尝试填充 testBeanListMap
属性 的 testBeanHolder
,但没有用。 testBeanListMap
已创建但为空。这是 URL 我正在尝试 http://localhost:8080/Struts2Test/test?testBeanHolder.testBeanListMap[0][0].value=1
表达式 testBeanHolder.testBeanListMap[0][0].value
不是有效的 OGNL 表达式。
有关允许的内容的完整参考,请参阅 here。
url http://localhost:8080/Struts2Test/test?testBeanHolder.testBeanListMap[0][0].value=1
将不起作用,因为您使用了错误的参数名称。因此 testBeanHolder.testBeanListMap[0][0].value
是映射到对象的参数名称,该对象具有复杂类型(集合的集合)的 属性。 Struts2 无法处理这种情况,。但是您可以用一个对象包装第二个集合并使用对象集合。名称将更改为 testBeanHolder.testBeanListMap[0].object[0].value
.
这是有效的代码,添加修改后的 类:
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