PDA

View Full Version : Passing java Pojo from Extjs to Spring Controller



dev_java
9 Aug 2010, 5:03 AM
Hi ,

I created a grid. On selection of the grid ,i am calling a function doUpdateData() which takes record of the datagrid as input. Using Ajax , how i will pass this pojo to the spring controller. I tried giving the record in params field of Ajax request. But on runtime am getting exception like 'Cannot Cast String to SampleBean' . SampleBean is the pojo used by me.


function doDBUpdate(record){
Ext.Ajax.request({
url: '../samplefiles/updateDB.htm',
method: 'POST',
params: {
sampledata: record // record is the selected record of the datagrid
},
success: function(){
alert('success');
},
failure: function(form, action){

} ,
scope: this,

});
}

On getting sample data in Spring controller , am getting run time error

public ModelAndView updateDB(HttpServletRequest request,
HttpServletResponse response) throws Exception {

Object obj=request.getParameter("sampledata");

SampleBean obj = (SampleBean ) obj; // casting error

ModelMap modelMap = new ModelMap();
return new ModelAndView("jsonView",modelMap);
}


How i can pass pojo to controller?

Condor
9 Aug 2010, 5:09 AM
Can Spring 3 do JSON to object mapping? I'm still on Spring 2.5 and I would use a AbstractCommandController with SampleBean as commandClass.

For that to work, your params would need to be:

params: record.data
(you might however need to configure your binder to match the datatypes)

dev_java
9 Aug 2010, 8:23 PM
Hi,

Am using Spring 3 and JSON to object mapping is possible.

Do u mean to say params: {sampledata: record.data } . I tried like this. As request.getParameter('sampledata') is returning String, am getting the cast exception now also.

If we give params like parms:record.data , how i can access it in controller class

Thanks

Condor
10 Aug 2010, 12:16 AM
If Spring 3 understands JSON then you could use:

jsonData: {sampledata: record.data}
But I assume you need some additional configuration to make Spring map the JSON onto your object.

It will definately be not as simple as:

SampleBean obj = (SampleBean) request.getParameter("sampledata");

psnprasad
28 Aug 2012, 3:55 AM
Hi,
Have you got the answer? I am also facing the same problem....

dev_java
28 Aug 2012, 4:07 AM
Hi,

I have used the following way to pass pojo value from extjs to controller.

In Ajax request , u have to give params as follows


params:{
sampledata: Ext.encode(record.data), // record is the selected record of grid
}


In the controller, get the sampledata from request and decode back



String sampleDetails = request.getParameter("sampledata");
JSONArray jsonArray = JSONArray.fromObject(sampleDetails );
List< POJO ClassName> sampleList = (List<POJO ClassName>) JSONArray.toCollection(jsonArray, POJO ClassName.class);



Hope this works..

snehalmasne
4 Aug 2014, 4:57 AM
Just remove the jsonData and type from my Ajax Request.


Instead add
params: {'id':record.get('id')} and on server side, you will get values with
request.getParameter("id").


It works!