Hi,
I would expose you a simple solution to encode an array of objects during the submission of a form on client side (in Javascript) and decode this same array on server side (in JAVA).
Client side
During the submission of a form, we create a parameter named MyArrayParam which will contain the array of objects:
12 | var form = this .up( 'form' ).getForm(); |
13 | if (form.isValid()== false ){ |
15 | Ext.MessageBox.hide(); |
17 | Ext.MessageBox.alert( 'Alert' , 'Some fields are invalid' ); |
21 | url: 'mySpringMVCController.do?action=handleUpdate' , |
24 | MyArrayParam: getArrayParam() |
26 | waitTitle: 'Please wait' , |
27 | waitMsg: 'Sending and processing the data...' , |
28 | success: function (form, action) { |
31 | ,invalid: function (form, action){ |
34 | ,failure: function (form, action) { |
We need a method to create the parameter MyArrayParam (for example by ranging the records in a store):
01 | function getArrayParam(){ |
03 | var allRecords = Ext.getCmp( 'myGridPanelID' ).getStore().getRange(); |
04 | for (i=0;i < allRecords.length;i++){ |
05 | var rec = allRecords[i]; |
06 | storeValues[i] = rec.data; |
08 | var myArrayParam = new MyArrayParam(); |
09 | myArrayParam.arrayItems = storeValues; |
10 | return Ext.encode(myArrayParam); |
14 | MyArrayParam = function () { |
Server side
First, we will create a class AnArrayItem for the mapping the fields of an record in store (ExtJs) :
1 | myArrayParam.arrayItems = storeValues; |
…
02 | * This class bind with Extjs from fields. |
04 | public class AnArrayItem { |
07 | private String id = null ; |
08 | private String type = null ; |
09 | private String reference = null ; |
…then, the command class MyParameterCommand containing the array of above class AnArrayItem named arrayItems:
02 | * This class bind with Extjs from fields. |
04 | public class MyParameterCommand { |
07 | private String globalId = null ; |
08 | private String globalType = null ; |
11 | private AnArrayItem[] arrayItems; |
Important note: The name arrayItems of array field in JAVA side
1 | private AnArrayItem[] arrayItems; |
and in EXTJS side
1 | MyArrayParam = function () { var arrayItems; } |
must be equal.
…finally, in the MVC controller, we retrieve the datas from the request and inject they in the JAVA bean MyParameterCommand:
Dans le controller Spring pour récupérer les données et les mettre dans un bean Java :
02 | public ModelAndView handleUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InterruptedException { |
05 | MyParameterCommand parameters = new MyParameterCommand(); |
06 | ServletRequestDataBinder binder = new ServletRequestDataBinder(parameters); |
11 | String myArrayParams = request.getParameter( "MyArrayParam" ); |
12 | if (myArrayParams!= null ){ |
13 | JSONObject jsonObject = JSONObject.fromObject(myArrayParams); |
15 | MyParameterCommand parametersTmp = (MyParameterCommand) JSONObject.toBean(jsonObject, MyParameterCommand. class ); |
17 | parameters.setArrayItems(parametersTmp.getArrayItems()); |
That’s all!!!!
Huseyin OZVEREN
Related
thanks