Struts2 associates a map inside an object with an action attribute

Part 1: There is an object (ObjectA) that has another object (ObjectB). Object B. has a hash map. This hash file has a string as a key, and another "ObjectC" object as a value. This hash is displayed in jsp using the s: iterator and s: text field, and it displays correctly. that is, the "values" inside the s: text field are correct, but the "name" is not. Now the problem occurs when changing the text field. How do we commit changed values ​​inside an ObjectC in an 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> 
+1
source share
1 answer

In your case, objB is a List associated with a HashMap , then one HashMap for each List element.

 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 will need two iterators and the following OGNL notation to refer to individual 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> 
0
source

All Articles