Struts2:更新Map中"List Of Objects"的值

Vic*_*kym 6 java collections struts2 treemap ognl

有一个对象ObjectA有一个列表ObjectB.TreeMap里面有一个ObjectB.它TreeMap有一个String关键字和List另一个对象ObjectC作为值.这个TreeMaplist内部已经显示在jsp使用s:iterators:textfield,它正在正确显示.即s:textfield中的"值"是正确的.现在,修改文本字段时出现问题.我们如何捕获动作类中ObjectC内的修改值?使用此处给出的代码,键("Key1")出现在操作中,但值为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....
Run Code Online (Sandbox Code Playgroud)

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> 
Run Code Online (Sandbox Code Playgroud)

HTML呈现: <input type="text" id="review-act1_objA_objB_0__allPlainFields_'Key1'__6__attr112" value="Correct Value" name="objA.objB[0].allPlainFields['Key1'][0].attr112">

eclipse的"VAriables"视图中的对象结构显示:

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   
Run Code Online (Sandbox Code Playgroud)

****在Eclipse"Variables"视图中,allPlainFields的值为**:**{Key1 =}

编辑(2013年2月27日):

试过这个,但没有奏效.这些值出现在jsp上,但是在提交时,它们不会起作用:

Action课堂上:

private TreeMap<String,ObjectCList> testTreeMap = new TreeMap<String,ObjectCList>();
//get,set and setting two keys in map "mykey1" and "mykey2"
Run Code Online (Sandbox Code Playgroud)

ObjectCList课堂上:

private ArrayList<ObjectC> paramMdlList;
 //default constructor, get, set
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

提交表单时,调用updateTreeMap方法action.地图被打印为提到这里:

public String updateTreeMap(){

    for (Map.Entry<String, ObjectCList> entry : testTreeMap.entrySet())
    {
        System.out.println(entry.getKey() + "/" + entry.getValue());
    }

    return SUCCESS;
Run Code Online (Sandbox Code Playgroud)

}

什么是"打印":mykey1/mykey2 /即空值

下面的屏幕显示了jsp中的值 屏幕显示值来自jsp

Ale*_*r M 6

根据您的最新更新.如果您使用的是TreeMapStruts2,则无法正确确定其中的元素类型.变更申报testTreeMapTreeMapMap.

private Map<String,ObjectCList> testTreeMap = new TreeMap<String,ObjectCList>();
Run Code Online (Sandbox Code Playgroud)

或者使用注释testTreeMap进行com.opensymphony.xwork2.util.Element注释以告诉Struts2地图中的元素是什么类型.

@Element(value = ObjectCList.class)
private TreeMap<String,ObjectCList> testTreeMap = new TreeMap<String,ObjectCList>();
Run Code Online (Sandbox Code Playgroud)


And*_*ios 5

我变得好奇并做了其他实验.

我发现,无论是List里面装的Lists和也Map的内线MapS(和所有的插值),声明为接口(List,Map),或者作为它们的实现(ArrayList,HashMap,TreeMap)被正确地处理XWork Converter.

所有测试用例都失败了.

也许这是我的错,如果是这样我们真的需要一些OGNL专家,因为在整个网络上没有什么可以谈论这个.

然后我尝试了我非常确定会有效的方法:以纯粹的方式将这些信息封装在自定义对象OOP.

它工作:)

代替

private ArrayList<ArrayList<String>> outerObjects;
Run Code Online (Sandbox Code Playgroud)

你可以在你的行动中使用

private ArrayList<OuterObject> outerObjects; 

/* GETTERS AND SETTERS */
public ArrayList<OuterObject> getOuterObjects() {
    return outerObjects;
}
public void setOuterObjects(ArrayList<OuterObject> outerObjects) {
    this.outerObjects = outerObjects;
}
Run Code Online (Sandbox Code Playgroud)

OuterObject定义:

/* ELSEWHERE IN YOUR PROJECT... */
public class OuterObject{
    private ArrayList<InnerObject> innerObjects;

    /* GETTERS AND SETTERS */
    public ArrayList<InnerObject> getInnerObjects() {
        return innerObjects;
    }
    public void setInnerObjects(ArrayList<InnerObject> innerObjects) {
        this.innerObjects = innerObjects;
    }       
}
Run Code Online (Sandbox Code Playgroud)

InnerObject定义:

public class InnerObject{
    String innerField;

    /* GETTERS AND SETTERS */       
    public String getInnerField() {         
        return innerField;
    }
    public void setInnerField(String innerField) {
        this.innerField = innerField;
    }

}
Run Code Online (Sandbox Code Playgroud)

Action的可选execute()方法用于测试预设值:

        InnerObject innerObj1 = new InnerObject();
        innerObj1.setInnerField("Inner Value 1");

        ArrayList<InnerObject> innerObjArrayList = new ArrayList<InnerObject>();
        innerObjArrayList.add(innerObj1);

        OuterObject outerObj1 = new OuterObject();      
        outerObj1.setInnerObjects(innerObjArrayList);

        outerObjects = new ArrayList<OuterObject>();
        outerObjects.add(outerObj1);
Run Code Online (Sandbox Code Playgroud)

JSP:

    <s:form>
        <s:textfield name="outerObjects[0].innerObjects[0].innerField" />
        <s:submit/>
    </s:form>
Run Code Online (Sandbox Code Playgroud)

(迭代时,简单地使用[%{#stat.index}]用于ListS和['%{#stat.index}']MapS)

相同的解决方案适用于各种可迭代结构(可能除了Guava需要.create()调用方法的东西之外).

当然这在每种情况下都不方便,在你的例子中你已经有了一个巨大的结构,这几乎会加倍,但是它有效,它是OOP,你的OGNL会更清晰(因为名字)而且似乎是唯一的办法.

注意:类必须是真正的独立类,而不是Inner Classes另一种OGNL无法自动装配对象的情况.

希望有所帮助


编辑

你需要的只是一个级别:

改变这个:

private TreeMap<String,List<ObjectC>> allPlainFields;
Run Code Online (Sandbox Code Playgroud)

对此

private TreeMap<String,ObjectX> allPlainFields;
Run Code Online (Sandbox Code Playgroud)

并创建一个ObjectX包含私有字段的文件List<ObjectC>.

它会工作.