Struts2:更新“对象列表"的值地图内 [英] Struts2: Updating the values of a "List Of Objects" inside a Map
问题描述
有一个对象ObjectA
,它有一个ObjectB
的列表.ObjectB
中有一个 TreeMap
.这个 TreeMap
有一个 String
作为键和一个 List
另一个对象 ObjectC
作为值.这个TreeMap
和里面的list
已经使用s:iterator
和s显示在
并且显示正确.即 s:textfield 中的值"是正确的.现在,修改文本字段时会出现问题.我们如何在 action 类中捕获 ObjectC 中修改后的值?使用此处给出的代码,键(Key1")出现在操作中,但值为空.jsp
上:textfield
There is an object ObjectA
which has a list of ObjectB
. There is a TreeMap
inside the ObjectB
. This TreeMap
has a String
as key and a List
of another object ObjectC
as value. This TreeMap
and the list
inside has been displayed on the jsp
using the s:iterator
and s:textfield
and it is being displayed correctly. i.e. the "values" inside the s:textfield are correct. Now, the problem arises when the textfield is modified. How do we capture the modified values inside ObjectC in the action class? With the code given here, the key ("Key1") comes in the action but the value is null.
Java 代码
public class ObjectA implements Serializable {
private Integer attr1;
private List<ObjectB> objB;
//...getters and setters....
public class ObjectB implements Serializable {
private Integer attr11;
private TreeMap<String,List<ObjectC>> allPlainFields;
// ...getters and setters....
public class ObjectC implements Serializable {
private Integer attr111;
public String attr112;
// ...getters and setters....
JSP 代码
<s:iterator value="objA.objB" var="currentObjB" status="currentGroupStatus">
<s:iterator value="#currentObjB.allPlainFields" var="parentMap" status="headerStatus">
<s:iterator value="#parentMap.value" var="fieldList" status="fieldStatus">
<s:textfield name="objA.objB[%{#currentGroupStatus.index}].allPlainFields['%{#parentMap.key}'][%{#fieldStatus.index}].attr112"/>
</s:iterator>
</s:iterator>
HTML 呈现:<input type="text" id="review-act1_objA_objB_0__allPlainFields_'Key1'__6__attr112" value="正确值" name="objA.objB[0].allPlainFields['Key1'][0].attr112">
eclipse变量"视图中的对象结构显示:
objA Object A (id=955)
objB ArrayList<E> (id=966)
elementData Object[10] (id=967)
[0] ObjectB (id=968)
allPlainFields TreeMap<K,V> (id=972)
comparator null
descendingMap null
entrySet TreeMap$EntrySet (id=979)
keySet null
modCount 1
navigableKeySet null
root TreeMap$Entry<K,V> (id=980)
size 1
values null
[1] ObjectB (id=969)
[2] ObjectB (id=970)
[3] ObjectB (id=971)
[4] null
[5] null
[6] null
[7] null
[8] null
[9] null
modCount 4
size 4
****在Eclipse变量"视图中,allPlainFields的值为**:** {Key1=}
****In the Eclipse "Variables" view, the value for allPlainFields is**:** {Key1=}
编辑(2013 年 2 月 27 日):
试过了,没用.这些值出现在 jsp 上,但在提交时,它们不会起作用:
Tried this but didn't work. The values appear on jsp but when submitted, they don't come in action:
在 Action
类中:
private TreeMap<String,ObjectCList> testTreeMap = new TreeMap<String,ObjectCList>();
//get,set and setting two keys in map "mykey1" and "mykey2"
在 ObjectCList
类中:
private ArrayList<ObjectC> paramMdlList;
//default constructor, get, set
在JSP
中:
<s:form id="test-map" method="post">
<s:iterator value="testTreeMap" var="pMap" status="hStatus">
<li><label><s:property value="%{#pMap.key}" /></label>
<s:iterator value="%{#pMap.value.paramMdlList}" var="pList" status="innerStatus">
<s:textfield name="testTreeMap['%{#pMap.key}'].paramMdlList[%{#innerStatus.index}].attr111"/>
<s:textfield name="testTreeMap['%{#pMap.key}'].paramMdlList[%{#innerStatus.index}].attr112"/>
</s:iterator>
</li>
</s:iterator>
<s:submit value=" " type='button' id="btnh1" action="saveTreeMap">
<span>Save TreeMap</span>
</s:submit>
</s:form>
提交表单时,调用action
的updateTreeMap
方法.该地图按照此处所述打印:
When the form is submitted, updateTreeMap
method of the action
is called. The map is printed as mentioned here :
public String updateTreeMap(){
for (Map.Entry<String, ObjectCList> entry : testTreeMap.entrySet())
{
System.out.println(entry.getKey() + "/" + entry.getValue());
}
return SUCCESS;
}
什么是打印":mykey1/mykey2/即空值
What is "printed" : mykey1/ mykey2/ i.e. null values
下面的屏幕显示了传入 jsp 的值
推荐答案
根据您的最新更新.如果您使用 TreeMap
Struts2 无法正确确定其中的元素类型.将 testTreeMap
的声明从 TreeMap
更改为 Map
.
According to your latest update. If you are using TreeMap
Struts2 cannot correctly determine type of elements inside it. Change declaration of testTreeMap
from TreeMap
to Map
.
private Map<String,ObjectCList> testTreeMap = new TreeMap<String,ObjectCList>();
或者用com.opensymphony.xwork2.util.Element
注解testTreeMap
来告诉Struts2里面的元素是什么类型的map.
Or annotate testTreeMap
with com.opensymphony.xwork2.util.Element
annotation to tell Struts2 what type are elements inside map.
@Element(value = ObjectCList.class)
private TreeMap<String,ObjectCList> testTreeMap = new TreeMap<String,ObjectCList>();
这篇关于Struts2:更新“对象列表"的值地图内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!