1

Part 1: There is an object (ObjectA) which has another object (ObjectB). There is a Hashmap inside the Object B. This hashmap has a string as key and another object "ObjectC" as value. This hashmap 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 but the "name" are not. Now, the problem arises when the textfield is modified. How do we capture the modified values inside ObjectC in the action class?

public class ObjectA implements Serializable {
    private Integer attr1;
    private List<ObjectB> objB;
    //...getters and setters....
public class ObjectB implements Serializable {
private Integer attr11;
private Map<String,ObjectC> myMap;
    // ...getters and setters....
public class ObjectC implements Serializable {
private Integer attr111;
public String attr112;
    // ...getters and setters....

jsp code:

<s:iterator value="#objB.myMap" var="fieldMap" status="fieldStatus">

<li><label><s:property value="#fieldMap.key"/></label><span>
<s:textfield name="<NOT SURE>" value="%{#fieldMap.value.attr12}"  /></span></li>



</s:iterator> 
Vickym
  • 137
  • 2
  • 10
  • By submitting a form? Not sure what the question is yet--are you saying you're using the correct notation in a form and it isn't working? – Dave Newton Feb 19 '13 at 13:49
  • @Dave jsp code has been added to the question. the value is being displayed correctly but when the same expression is put in the "name" attribute, the value doesn't come in the action when the form is submitted with the modified values – Vickym Feb 19 '13 at 14:07
  • Names are names; you need to name it after the object graph: all OGNL does is turn expressions into calls. It has no way of knowing that `fieldMap` isn't actually a property named `fieldMap`. – Dave Newton Feb 19 '13 at 14:10
  • So should the name be objA.objB.myMap('%{#fieldMap.key}').attr112 ? – Vickym Feb 19 '13 at 14:18
  • Dave, thank you for looking into this. – Vickym Feb 20 '13 at 06:25

1 Answers1

0

In your case, objB is a List contanining an HashMap, then one HashMap for every element of the List.

objA
  |--- objB[0]
    |-- objC[A]
    |-- objC[B]
    |-- objC[C]
  |--- objB[1]
    |-- objC[X]
    |-- objC[Y]
    |-- objC[Z]
  |--- objB[n]
    |-- objC[N1]
    |-- objC[N2]
    |-- objC[N3]

Then you need two iterators and the following OGNL notation, to refer to the single elements with the name attribute:

<s:iterator value="objA.objB" var="listRow" status="listStatus">
<!-- Iterating the List -->
    <s:iterator value="#listRow.myMap" var="mapRow" >
    <!-- Iterating the Map -->
        <li>
            <label>
                <s:property value="#mapRow.key"/>
            </label>
            <span>
                <s:textfield value="%{#mapRow.value.attr12}"  
                  name="objA.objB[#listStatus.index].myMap[#mapRow.key].attr112"
                />
            </span>
        </li>
    </s:iterator> 
</s:iterator> 
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • Andrea, Apologies...The IDE object structure and the console output has been appended to the original question. Initially I thought, a map would be sufficient but looks like it has to be a TreeBasedTable from guava which makes it even more complicated. Could you please have a look? Although a TreeBasedTable is a map inside a map, let me know if you would like me to open another question for this. I tried doing mymap[#outerkey][#innerkey].attr112 but, it didn't work. – Vickym Feb 19 '13 at 17:33
  • Uh, nice :D I've never used that stuff, but if you take a look at how you access your objects in Java, then it is exactly the same (with different syntax, but same semantics) in OGNL. I can suggest you to start with a single TreeBasedTable object at root level (sibling of objA), just to test that you can fetch and return its value correctly from JSP using OGNL; once avhieved that, start deep diving the double / triple iteration with all its complexity. Hope that helps – Andrea Ligios Feb 19 '13 at 17:42
  • Andrea, for my map question your solution would have worked. I've opened another discussion for guava table. Thank you for sparing your time to look at this. [link]http://stackoverflow.com/q/14973381/2087216 – Vickym Feb 20 '13 at 06:20