i using play framework java. , have jquery ajax post data (string representation of jsonnode). writing action method in controller class serving ajax call.
the problem facing that, data sending jquery ajax has 10 textfield values. , have 3 bean classes (viz., myclass1,myclass2,myclass3) have these 10 variables capture textfield values. if had single bean(say,myclass1`) 10 variables defined in it, write like:
jsonnode json = request().body().asjson(); myclass1 obj1 = json.fromjson(json, myclass1.class); and access values obj1.
but have 3 beans , in first bean i've 3 variables, in second 5 variables, , in third 2 variables. creates problem.
how these 10 textfield values in myclass1 obj1, myclass2 obj2, myclass3 obj3 in proper way , required. in other words how achieve spliting of jsonnode. or similar appropriate values in 3 beans.
any or clue appreciated. in advance. can done these?
well, answer question pretty simple. did added following 2 lines in bean myclass1:
public myclass2 m2; public myclass3 m3; and kept code in controller's json method same earlier, i.e.:
jsonnode json = request().body().asjson(); myclass1 obj1 = json.fromjson(json, myclass1.class); and in jquery created complex json var (note: have @ data1 below):
var data1 = { "datafield1": datafield1, "datafield2": datafield2, "datafield3": datafield3, "data2": data2, "data3": data3 } var data2 = { "datafield4": datafield4, "datafield5": datafield5, "datafield6": datafield6, "datafield7": datafield7, "datafield8": datafield8 } var data3 = { "datafield9": datafield9, "datafield10": datafield10 } and while making ajax call passed data1 as:
$.ajax({ url: '/myurl/data1', type: 'post', data: json.stringify(data1), contenttype: "application/json", .. .. .. }); now in controller's json method, access obj1.m2 , obj1.m3and solve problem. (no need think spliting jsonnode.)
Comments
Post a Comment