PDA

View Full Version : [SOLVED] How to pass a record to java controller ?



rimbaudsolaris
10 Jun 2010, 4:48 AM
I would like to pass the record below via the params to the a java controller, and this after a submit.

The controller get the record from
Object data = request.getParameter("uHoliday");

var uHoliday = new holiday({
id: '50',
holidayDesc: 'Test',
holidayDate: '01/01/2000'
});

params:{holiday: uHoliday, cmd:'edit'},Thanks a lot for your help

Animal
10 Jun 2010, 5:00 AM
Submit the form's data as jsonData.

In Java, parse the POST body into an org.json.JSONObject

rimbaudsolaris
10 Jun 2010, 5:32 AM
Ok I did that on my controller




Object data = request.getParameter("holiday");
Holiday holiday = getHolidayFromJSON(data);


/**
* Transform json data format into Holiday object
* @param data - json data from request
* @return
*/
public static Holiday getHolidayFromJSON(Object data){
JSONObject jsonObject = JSONObject.fromObject(data);
Holiday newHoliday = (Holiday) JSONObject.toBean(jsonObject, Holiday.class);
return newHoliday;
}


From my jsp



var holiday = Ext.data.Record.create([
{name: 'id'},
{
name: 'holidayDesc',
type: 'string'
}, {
name: 'holidayDate',
dateFormat: 'm/d/Y'
}
]);

var uHoliday = new holiday({
id: '50',
holidayDesc: 'Test',
holidayDate: '01/01/2000'
});

var obj = Ext.util.JSON.encode(uHoliday);

Pass through obj params:{holiday: obj, cmd:'edit'},




But I get the error below when I tried to do
Holiday newHoliday = (Holiday) JSONObject.toBean(jsonObject, Holiday.class);



2010-06-10 15:26:01,022 WARN [net.sf.json.JSONObject] - Tried to assign property phantom:java.lang.Boolean to bean of class eu.europa.ec.sg.sybiladmin.domain.Holiday

I don't know why inspecting object from my controller, I get the these values
"phantom":true,"id":"ext-record-1"

{"phantom":true,"id":"ext-record-1","data":{"id":"50","holidayDesc":"Test","holidayDate":"01/01/2000"}}

rimbaudsolaris
10 Jun 2010, 6:01 AM
The real exception is the
Unknown property 'data'


2010-06-10 15:51:13,892 WARN [net.sf.json.JSONObject] - Tried to assign property phantom:java.lang.Boolean to bean of class eu.europa.ec.sg.sybiladmin.domain.Holiday
10-juin-2010 15:51:13 org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: "Servlet.service()" pour la servlet sybiladmin a généré une exception
java.lang.NoSuchMethodException: Unknown property 'data'

Animal
10 Jun 2010, 6:12 AM
You cannot just JSON encode a Record. The object graph is unknown (to you as an app developer. In particular, if it's in a Store, it will be linked to the Store, and so there's a huge object graph right there)

You must JSON encode its individual fields: the data property. As you can plainly see by looking at that JSON.

rimbaudsolaris
10 Jun 2010, 6:23 AM
Oups....

It's because I have to use this decode


var obj = Ext.util.JSON.encode(uHoliday.data); and not this one

and it return the right object to {"id":"50","holidayDesc":"Test","holidayDate":"01/01/2000"}
to do this


Holiday newHoliday = (Holiday) JSONObject.toBean(jsonObject, Holiday.class); and of course it works because Holiday contains these three fields

private Long id;
private String holidayDate;
private String holidayDesc;


If I use the encode (like below) directly with the record uHoliday without pointing to data, I get the error.


var obj = Ext.util.JSON.encode(uHoliday);

So it's solved now ;-)

Thanks for your help