Forum

November 2nd, 2014
A A A
Avatar

Lost password?
Advanced Search

— Forum Scope —




— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

The forums are currently locked and only available for read only access
sp_Feed Topic RSS sp_TopicIcon
How do I access JSON string in jsp?
26/01/2010
19:06
Avatar
mikesmith
Member
Members
Forum Posts: 47
Member Since:
26/01/2010
sp_UserOfflineSmall Offline

Hi. Im implenting struts &jquery.

my action will return a JSON string to my jsp. What do I need to do to access the JSON string after the ajax call has already been made?

I am implementing the grid. I have added serveral key/value pairs in my JSON string that will NOT be in the grid.

And I want to use these extra values to implement some logic after the ajax callis complete.

I have put onCompleteTopics="complete" in my grid. This calls:

    $.subscribe('complete', function(data){
            var res = jQuery.getJSON();

             alert(rest);
    });

Howevers, res is not my JSON string that is returned to the jgrid by my action.

Any one with ideas how I can access the json string that org.apache.struts2.json.JSONUtil serlializes and sends to the jsp?

merci

28/01/2010
17:30
Avatar
tony
Sofia, Bulgaria
Moderator
Members

Moderators
Forum Posts: 7721
Member Since:
30/10/2007
sp_UserOfflineSmall Offline

Hello,

Use loadComplete - the parameter passed to it is the request

loadComplete : function(request)

{

// do somthing here with the request

}

Best Regards

Tony

For professional UI suites for Java Script and PHP visit us at our commercial products site - guriddo.net - by the very same guys that created jqGrid.

29/01/2010
12:41
Avatar
mikesmith
Member
Members
Forum Posts: 47
Member Since:
26/01/2010
sp_UserOfflineSmall Offline

I tried doing this and when I alert(request.responseText) I get undefined. and also request.status gives me undefined.

I am new to all this so your help is appreciated.

01/02/2010
08:51
Avatar
tony
Sofia, Bulgaria
Moderator
Members

Moderators
Forum Posts: 7721
Member Since:
30/10/2007
sp_UserOfflineSmall Offline

Hello,

You shoud use request directly, and not request.responsText

try alert(request);

Regards

Tony

For professional UI suites for Java Script and PHP visit us at our commercial products site - guriddo.net - by the very same guys that created jqGrid.

02/02/2010
01:49
Avatar
mikesmith
Member
Members
Forum Posts: 47
Member Since:
26/01/2010
sp_UserOfflineSmall Offline

I tried that but the alert was: [object Object] 🙁

05/02/2010
11:24
Avatar
tony
Sofia, Bulgaria
Moderator
Members

Moderators
Forum Posts: 7721
Member Since:
30/10/2007
sp_UserOfflineSmall Offline

Hello,

Sorry it was my mistake. Of course it is a object.

If you know some property of this object you can

alert(request.myobjectname);

Best Regards

Tony

For professional UI suites for Java Script and PHP visit us at our commercial products site - guriddo.net - by the very same guys that created jqGrid.

05/01/2011
19:01
Avatar
aarati
Member
Members
Forum Posts: 8
Member Since:
05/01/2011
sp_UserOfflineSmall Offline

Hi Tony,

I have a similar issue as mentioned in the discussion above. Making use of the following in the application:

1) Struts 1.2

2) JQGrid 3.8.2

3) JQuery 1.4.4

The Struts action method returns a json object after obtaining data from the database. See code below:

public ActionForward getJSONData(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {

...
...
 JSONObject jsonObj = createJSONTestData();
 response.setHeader("X-JSON", jsonObj.toString());
 System.out.println("Final JSON OBj: " + jsonObj.toString());
 return mapping.findForward(sForward);
}

/*
  *This piece of code is the same as the php code mentioned in one of the examples
  *provided in jqgrid demos
*/
public JSONObject createJSONTestData() {
....
....
//Hit the database and obtain the resultset and create the json object

 JSONObject responcedata=new JSONObject();
 net.sf.json.JSONArray cellarray=new net.sf.json.JSONArray();
 responcedata.put("total","3");
 responcedata.put("page",cpage);
 responcedata.put("records","2");

 net.sf.json.JSONArray cell=new net.sf.json.JSONArray();
 net.sf.json.JSONObject cellobj=new net.sf.json.JSONObject();

 int i=1;
 while(rs.next()){
  cellobj.put("id",rs.getInt(1));
  cell.add(rs.getInt(1));
  cell.add(rs.getString(2));
  cell.add(rs.getString(3));
  cellobj.put("cell",cell);
  cell.clear();
  cellarray.add(cellobj);
  i++;
        }
  PrintWriter out = response.getWriter();
  responcedata.put("rows",cellarray);
   
  return responcedata;

}

 

The struts-config.xml action forward mapping points to an empty.jsp that has no specific code.

The jsp file formulates the jqgrid as shown below:

$(document).ready(function(){
$("#load_callback1").click(function(){
jQuery("#list10").jqGrid({
 
url:'/TestJSON/testAction.do?method=getJSONData',
 datatype: "json",
    colNames:['Invoice Number','Invoice Date','Invoice Name'],
    colModel:[
     {name:'id',index:'id', id:'id',width:90},
     {name:'invdate',index:'invdate', width:120},
     {name:'invname',index:'invname', width:200}
    ],
    rowNum:10,
    autowidth: true,
    rowList:[10,20,30],
    pager: jQuery('#pager1'),
    sortname: 'invname',
    sortorder: "desc",
    caption:"JSON Example"
}).navGrid('#pager1',{edit:false,add:false,del:false}); 
});
});

The grid comes up with no data. A similar example works well with the doGet method of a servlet.

Questions:

1) How do we obtain the json object returned from a struts action method?

2) Is there any process to be done in the empty.jsp file to handle the response object and obtain the json data?

Not sure if I posted the question under the right topic. Awaiting for a clarification at the earliest. Thank you.

Regards,

Aarati

 

Regards,

Aarati

Forum Timezone: Europe/Sofia

Most Users Ever Online: 715

Currently Online:
53 Guest(s)

Top Posters:

OlegK: 1255

markw65: 179

kobruleht: 144

phicarre: 132

YamilBracho: 124

Renso: 118

Member Stats:

Guest Posters: 447

Members: 11373

Moderators: 2

Admins: 1

Forum Stats:

Groups: 1

Forums: 8

Topics: 10592

Posts: 31289

Newest Members:

, razia, Prankie, psky, praveen neelam, greg.valainis@pa-tech.com

Moderators: tony: 7721, Rumen[Trirand]: 81

Administrators: admin: 66

Comments are closed.
Privacy Policy   Terms and Conditions   Contact Information