Struts2: Updating the values of a "List Of Objects" inside a 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>();

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>();

I've become curious and did other experiments.

I found out that neither Lists inside Lists, nor Maps inside Maps (and all the interpolations), declared as interface (List, Map), or as their implementations (ArrayList, HashMap, TreeMap) are correctly handled by the XWork Converter.

All the test cases have failed.

Maybe it's my fault, if so we really need some OGNL Experts here, because in the whole web there's nothing talking about this.

Then i tried what I was pretty sure that would have worked: encapsulating this informations in custom objects, in pure OOP way.

And it worked :)

Instead of

private ArrayList<ArrayList<String>> outerObjects;

you can use in your Action

private ArrayList<OuterObject> outerObjects; 

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

the OuterObject definition:

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

the InnerObject definition:

public class InnerObject{
    String innerField;

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

}

the optional execute() method of your Action to test the preset values:

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

the JSP:

    <s:form>
        <s:textfield name="outerObjects[0].innerObjects[0].innerField" />
        <s:submit/>
    </s:form>

(when iterating, simply use [%{#stat.index}] for Lists and ['%{#stat.index}'] for Maps)

The same solution is applicable for every kind of iterable structure (probably except the Guava stuff that needs the .create() method to be invoked).

Of course this is not handy in every case, in your example you have already a huge structure and this will almost double it, but it works, it's OOP, your OGNL will be clearer (because of names) and however seems to be the only way.

Note: the Classes must be real stand-alone Classes, not Inner Classes, another case where OGNL fails to autowire the objects.

Hope that helps


EDIT

What you need then, is only one more level:

change this:

private TreeMap<String,List<ObjectC>> allPlainFields;

to this

private TreeMap<String,ObjectX> allPlainFields;

and create an ObjectX containing a private field that is a List<ObjectC>.

It will work.