6

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 Code

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 Code

<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 rendered: <input type="text" id="review-act1_objA_objB_0__allPlainFields_'Key1'__6__attr112" value="Correct Value" name="objA.objB[0].allPlainFields['Key1'][0].attr112">

The object structure in the "VAriables" view of eclipse shows:

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   

****In the Eclipse "Variables" view, the value for allPlainFields is**:** {Key1=}

EDIT(27-Feb-2013):

Tried this but didn't work. The values appear on jsp but when submitted, they don't come in action:

In Action class:

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

In ObjectCList class:

private ArrayList<ObjectC> paramMdlList;
 //default constructor, get, set

In 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>

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;

}

What is "printed" : mykey1/ mykey2/ i.e. null values

The screen below shows values coming in jsp screen showing values come in jsp

Community
  • 1
  • 1
Vickym
  • 137
  • 2
  • 10
  • possible duplicate of [Struts2 binding a map inside an object to an action attribute](http://stackoverflow.com/questions/14959003/struts2-binding-a-map-inside-an-object-to-an-action-attribute) – Roman C Feb 21 '13 at 16:47
  • @Roman, The other link was for Map inside a list. This one is for List inside a Map inside a list. Logically, this should work in similar ways but surprisingly it's not. – Vickym Feb 21 '13 at 16:58
  • Did you receive the answer from the first question? Everytime you change the structure we should duplicate the code of each other? Why to ask the same question several times. Earn rep via answering questions, then spend it on bounty. – Roman C Feb 21 '13 at 17:05
  • 1
    @Roman, Yes I did receive the answer to the first question and I did accept it. Looks like one more level of a collection inside is not working. It looks like the same question but it isn't. Have you faced such a problem earlier? – Vickym Feb 21 '13 at 17:41
  • I don't understand, as for me I never, never use such expressions, unnecessary complexity lead to bugs and months, years to work. – Roman C Feb 21 '13 at 17:46

2 Answers2

6

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>();
Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
  • 1
    It wouldn't have been possible to reach so close to the solution without the efforts of Andrea Ligios! Thank you Andrea! – Vickym Feb 27 '13 at 11:23
  • Just out of curiosity, would this element notation have worked for a `guava TreeBasedTable`? This is where this started and the earlier `TreeBasedTable` was replaced with map of lists. A `TreeBasedTable` hasn't exposed the default constructor and has a `.create` method instead. ex: `@Element(value = TreeBasedTable.class) Table tableRepresentation = TreeBasedTable.create();` – Vickym Feb 27 '13 at 18:42
5

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.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • Will try it out. However, When I changed the `s:textfield` to `` , I got an error in the logs which said: _Unexpected Exception caught setting 'objA.objB[0].allPlainFields.get('Key1').get(0).attr112' on 'class com.vm.action.Act1: Error setting expression 'objA.objB[0].allPlainFields.get('Key1').get(0).attr112' with value ['testval', ]_. Please notice an addition "," and a space next to testval. This is the closest I've got till now;OGNL tried something :) – Vickym Feb 21 '13 at 18:16
  • Vicky, I've got closer many times (for example there are differences between List> and ArrayList>; appearently, in the first case OGNL has problems trying to "guess" the correct implementation for the interface... btw, thinking about it, your structure won't "almost double", you just need only ONE more level. I'm updating the answer right now with it. – Andrea Ligios Feb 22 '13 at 09:08
  • @Andrea, I had already added one extra layer based on your first response and I think, it shouldn't matter(?) Here are the 2 different errors I got with 2 different mappings at the Map level: **Scenario1**: Unexpected Exception caught setting `'rqPGPrmMdl.rqGrp[1].pFWrap.allPFields['DefaultHeader'].paramMdlList[7].pValue'` on 'class `com.hm.action.Act1`: Error setting expression `'rqPGPrmMdl.rqGrp[1].pFWrap.allPFields['DefaultHeader'].paramMdlList[7].pValue' with value '[Ljava.lang.String;@3b9b2222'` `ognl.NoSuchPropertyException: java.lang.String.paramMdlList` – Vickym Feb 22 '13 at 14:27
  • **Scenario2**: Unexpected Exception caught setting `'rqPGPrmMdl.rqGrp[1].pFWrap.allPFields.get('DefaultHeader').paramMdlList[5].pValue'` on 'class `com.hm.action.Act1`: Error setting expression `'rqPGPrmMdl.rqGrp[1].pFWrap.allPFields.get('DefaultHeader').paramMdlList[5].pValue'` with value `'[Ljava.lang.String;@3c67312'` `ognl.OgnlException: source is null for getProperty(null, "paramMdlList")` – Vickym Feb 22 '13 at 14:30
  • Check carefully *every* `getter` and `setter`: http://stackoverflow.com/questions/5064366/ognl-ognlexception-target-is-null-for-setproperty – Andrea Ligios Feb 22 '13 at 15:01
  • checked every getter and setter ..no issues some it gets the key in the action "Default Header" but no values... – Vickym Feb 22 '13 at 16:19
  • @AndreaLigios Updated the question with further findings. Had to take a break from this to finish something else. – Vickym Feb 27 '13 at 09:22
  • 1
    @AndreaLigios : The final one step given by Aleksandr M worked. However, it wouldn't have been possible to reach so close to the solution without your efforts! Thanks a ton!Is there anything on SO to appreciate the great efforts you have taken? – Vickym Feb 27 '13 at 11:21