PDA

View Full Version : send multiple / additional parameters to Jersey configured service method using model



samu_dra_nil
6 Nov 2012, 6:13 AM
Hello There,
I have tried a lot to send additional parameters to Jersey configured service methods from model.save(). extraParams will not work in this case, because we are using DaoHandler (which is not a servlet, but a simple java class) in server side and the model.save() must land to the exact method. But I didn't find any solution in the web and ultimately find a solution of my own. This trick really works for me.

----------The Model----------
Ext.define('MyApp.model.MyTestModel', {
extend: 'Ext.data.Model',

fields: [
{
name: 'dtoName',
type: 'string'
},
{
name: 'errorMessage',
type: 'string'
},
{
name: 'empId',
type: 'int'
},
{
name: 'accountsList',
type: 'auto'
},
{
name: 'countryNamesList',
type: 'auto'
},
{
name: 'accountRecord',
type: 'auto'
}
],

proxy: {
type: 'rest',
url: 'http://localhost:8888/DirectExtJs/rest/QuickReg/myTestMethod',
headers: {
'Content-Type': 'application/json'
},
reader: {
type: 'json'
}
}
});
--------End of Model----------

-----Button Click Handler--------
var myAccountRecord = function (firstName){
this.firstname = firstName;
this.lastname = Ext.getCmp('txtAccountLastName').value;
this.startdate = Ext.getCmp('dtAccountStartDate').value;
this.accountdesc = Ext.getCmp('txtAccountDescription').value;
};
var accountData = new myAccountRecord('SAMUDRANIL');
var accountData1 = new myAccountRecord('SAMUDRANIL1111');
var accountData2 = new myAccountRecord('SAMUDRANIL2222');
var accountData3 = new myAccountRecord('SAMUDRANIL3333');
var accountData4 = new myAccountRecord('SAMUDRANIL4444');

var accountList = [accountData1,accountData2,accountData3,accountData4];

var myTestModel = Ext.create('MyApp.model.MyTestModel');
myTestModel.set('dtoName', "MyDTO");
myTestModel.set('errorMessage', "This is a error");
myTestModel.set('empId', 10);
myTestModel.set('accountsList', accountList);
myTestModel.set('accountRecord', accountData);

var jsonStr = Ext.JSON.encode(accountData);
var jsonStr2 = Ext.JSON.encode(accountData); //this can be any other object

var newURL = myTestModel.proxy.url;
newURL = newURL + '/' + jsonStr + '/' + jsonStr2;
myTestModel.proxy.url = newURL;

myTestModel.save({
success : function(quickRegModel, operation){
var jsonData = Ext.JSON.decode(operation.response.responseText);
Ext.Msg.alert('Success', 'Customer Created Successfully');
console.log(jsonData);
},
failure: function(response, operation) {
console.log(operation);
alert('Customer Creation Failed.');
}
});
------End of Button Click Handler------

-----Service class(DaoHandler)--------
@Service("QuickRegService")
@Path("/QuickReg")
public class JCQuickRegistrationUnicornDAOHandler
{
@POST
@Path("/myTestMethod/{arnab}/{samudra}")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) // JERSEY
public static String myTestMethod( MyTestDto testDto, @PathParam("arnab") String mystr, @PathParam("samudra") String yourstr) throws Exception
{
Gson gson = new Gson();
CcAccount acc1 = gson.fromJson(mystr, CcAccount.class);
CcAccount acc2 = gson.fromJson(yourstr, CcAccount.class);

testDto.setDtoName("Amar DTO@" + acc1.getFirstname() + "@" + acc2.getLastname());

String jsonString = gson.toJson(testDto);

return jsonString;

}
}
-----End of Service class(DaoHandler)--------

Hope This will help you............... :)

mitchellsimoens
8 Nov 2012, 7:03 AM
Tried passing in params?


record.save({
params : {
foo : 'bar'
}
});