Tuesday, 15 February 2011

java - Struts2: Updating the values of a "List Of Objects" inside a Map -



java - Struts2: Updating the values of a "List Of Objects" inside a Map -

there object objecta has list of objectb. there treemap within objectb. treemap has string key , list of object objectc value. treemap , list within has been displayed on jsp using s:iterator , s:textfield , beingness displayed correctly. i.e. "values" within s:textfield correct. now, problem arises when textfield modified. how capture modified values within objectc in action class? code given here, key ("key1") comes in action value null.

java code

public class objecta implements serializable { private integer attr1; private list<objectb> objb; //...getters , setters.... public class objectb implements serializable { private integer attr11; private treemap<string,list<objectc>> allplainfields; // ...getters , setters.... public class objectc implements serializable { private integer attr111; public string attr112; // ...getters , 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 construction in "variables" view of eclipse shows:

obja object (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 eclipse "variables" view, value allplainfields is:* {key1=}

edit(27-feb-2013):

tried didn't work. values appear on jsp when submitted, don't come in action:

in action class:

private treemap<string,objectclist> testtreemap = new treemap<string,objectclist>(); //get,set , setting 2 keys in map "mykey1" , "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 form submitted, updatetreemap method of action called. map printed mentioned here :

public string updatetreemap(){ (map.entry<string, objectclist> entry : testtreemap.entryset()) { system.out.println(entry.getkey() + "/" + entry.getvalue()); } homecoming success;

}

what "printed" : mykey1/ mykey2/ i.e. null values

the screen below shows values coming in jsp

according latest update. if using treemap struts2 cannot correctly determine type of elements within it. alter declaration of testtreemap treemap map.

private map<string,objectclist> testtreemap = new treemap<string,objectclist>();

or annotate testtreemap com.opensymphony.xwork2.util.element annotation tell struts2 type elements within map.

@element(value = objectclist.class) private treemap<string,objectclist> testtreemap = new treemap<string,objectclist>();

java collections struts2 treemap ognl

No comments:

Post a Comment