Results 1 to 7 of 7

Thread: Passing java Pojo from Extjs to Spring Controller

  1. #1
    Sencha User
    Join Date
    Jun 2010
    Posts
    56

    Question Passing java Pojo from Extjs to Spring Controller

    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.

    Code:
    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
    Code:
    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?

  2. #2
    Sencha User
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,245

    Default

    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:
    Code:
    params: record.data
    (you might however need to configure your binder to match the datatypes)

  3. #3
    Sencha User
    Join Date
    Jun 2010
    Posts
    56

    Default

    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

  4. #4
    Sencha User
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,245

    Default

    If Spring 3 understands JSON then you could use:
    Code:
    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:
    Code:
    SampleBean obj = (SampleBean) request.getParameter("sampledata");

  5. #5

    Default Having the same problem....

    Hi,
    Have you got the answer? I am also facing the same problem....

  6. #6
    Sencha User
    Join Date
    Jun 2010
    Posts
    56

    Default

    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
    Code:
     params:{
       sampledata: Ext.encode(record.data), // record is the selected record of grid
    }
    In the controller, get the sampledata from request and decode back

    Code:
    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..

  7. #7
    Sencha Premium Member
    Join Date
    Jul 2014
    Location
    Pune, India
    Posts
    12

    Default

    Just remove the jsonData and type from my Ajax Request.


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

Similar Threads

  1. passing data from java class to ExtJs' treepanel
    By pirusira in forum Ext 3.x: Help & Discussion
    Replies: 10
    Last Post: 24 Jun 2010, 1:58 AM
  2. Passing variables from ExtJS to Pylons controller
    By jdobrowski in forum Ext 2.x: Help & Discussion
    Replies: 5
    Last Post: 26 Aug 2009, 11:18 AM
  3. Passing arguments to Spring controller
    By ejml in forum Ext 2.x: Help & Discussion
    Replies: 0
    Last Post: 26 Feb 2009, 7:25 AM
  4. Spring Controller
    By erwin in forum Community Discussion
    Replies: 6
    Last Post: 7 Aug 2008, 7:06 PM
  5. Spring Controller
    By Ilan in forum Sencha Ext JS Q&A
    Replies: 1
    Last Post: 2 Mar 2008, 8:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •