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_Related Related Topics sp_TopicIcon
Obtaining JSON object from struts action response
06/01/2011
07:04
Avatar
aarati
Member
Members
Forum Posts: 8
Member Since:
05/01/2011
sp_UserOfflineSmall Offline

Hello,

I get an empty grid when trying to obtain a json object from struts action method.

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?

Awaiting for a clarification at the earliest. Thank you.

Regards,

Aarati

06/01/2011
09:26
Avatar
aarati
Member
Members
Forum Posts: 8
Member Since:
05/01/2011
sp_UserOfflineSmall Offline

Hello,

Finally got it to work Smile.

Made slight modifications to the existing code:

Struts Action:

1) Earlier was returning to an empty jsp page with no specific code. Now returning null.

2) Using the write method of the jsonObj to write the response data.

The action method now looks like the following:

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



 JSONObject jsonObj = createJSONTestData();

System.out.println("Final JSON OBj: " + jsonObj.toString()); 

jsonObj.write(response.getWriter());

return null;
 }

 

--------------------------------------------------------------------------------------------------------------------------

JSP:

JSP Code to populate the grid is shown below:

$(document).ready(function(){
$.ajax(
    {
       type: "POST",
       url:'  /TestJSON/testAction.do?method=getJSONData'
,
       data: "",
       dataType: "json",
       success: function(result)
       {
            alert("success");
            alert("result: " + result.total);
            jQuery("#list10").jqGrid({
            url:'  /TestJSON/testAction.do?method=getJSONData',
                datatype: 'json',
                mtype: 'POST',
                colNames:['Invoice Number','Invoice Date','Invoice Name'],
         colModel:[
             {name:'id',index:'id', id:'name',width:90},
             {name:'invdate',index:'invdate', width:120},
             {name:'invname',index:'invname', width:200}
     ],
                pager: jQuery('#pager1'),
                rowNum: 5,
                rowList: [5, 10, 20, 50],
                viewrecords: true
            })
       },
       error: function(x, e)
       {
            alert("errorddd " + x.readyState + " "+ x.status +" "+ e);
            if (x.readyState == 4) {
             var response = x.responseText;
             alert(response);
            }
       }
    });
setTimeout(function() {$("#list10").jqGrid('setGridParam',{datatype:'json'}); },50);
});

--------------------------------------------------------------------------------------------------------------------------

 If any one faces any problem with the above mentioned implementation please post a reply to this topic.

Regards,

Aarati

20/03/2011
12:14
Avatar
kirangentlebreeze1987
New Member
Members
Forum Posts: 1
Member Since:
20/03/2011
sp_UserOfflineSmall Offline

aarati said:

Hello,

Finally got it to work Smile.

Made slight modifications to the existing code:

Struts Action:

1) Earlier was returning to an empty jsp page with no specific code. Now returning null.

2) Using the write method of the jsonObj to write the response data.

The action method now looks like the following:

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



 JSONObject jsonObj = createJSONTestData();

System.out.println("Final JSON OBj: " + jsonObj.toString()); 

jsonObj.write(response.getWriter());

return null;
 }

 

--------------------------------------------------------------------------------------------------------------------------

JSP:

JSP Code to populate the grid is shown below:

$(document).ready(function(){
$.ajax(
    {
       type: "POST",
       url:'  /TestJSON/testAction.do?method=getJSONData'
,
       data: "",
       dataType: "json",
       success: function(result)
       {
            alert("success");
            alert("result: " + result.total);
            jQuery("#list10").jqGrid({
            url:'  /TestJSON/testAction.do?method=getJSONData',
                datatype: 'json',
                mtype: 'POST',
                colNames:['Invoice Number','Invoice Date','Invoice Name'],
         colModel:[
             {name:'id',index:'id', id:'name',width:90},
             {name:'invdate',index:'invdate', width:120},
             {name:'invname',index:'invname', width:200}
     ],
                pager: jQuery('#pager1'),
                rowNum: 5,
                rowList: [5, 10, 20, 50],
                viewrecords: true
            })
       },
       error: function(x, e)
       {
            alert("errorddd " + x.readyState + " "+ x.status +" "+ e);
            if (x.readyState == 4) {
             var response = x.responseText;
             alert(response);
            }
       }
    });
setTimeout(function() {$("#list10").jqGrid('setGridParam',{datatype:'json'}); },50);
});

--------------------------------------------------------------------------------------------------------------------------

 If any one faces any problem with the above mentioned implementation please post a reply to this topic.

Regards,

Aarati


i do have some problem populating a json object values to my grid 

can you help me???

24/03/2011
05:36
Avatar
aarati
Member
Members
Forum Posts: 8
Member Since:
05/01/2011
sp_UserOfflineSmall Offline

Hi Kiran,

Which version of struts are you using? What is the problem that you are facing?

Regards,

Aarati

26/04/2011
07:39
Avatar
moin_mkhan
New Member
Members
Forum Posts: 1
Member Since:
26/04/2011
sp_UserOfflineSmall Offline

aarati said:

Hello,

Finally got it to work Smile.

Made slight modifications to the existing code:

Struts Action:

1) Earlier was returning to an empty jsp page with no specific code. Now returning null.

2) Using the write method of the jsonObj to write the response data.

The action method now looks like the following:

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



 JSONObject jsonObj = createJSONTestData();

System.out.println("Final JSON OBj: " + jsonObj.toString()); 

jsonObj.write(response.getWriter());

return null;
 }

 

--------------------------------------------------------------------------------------------------------------------------

JSP:

JSP Code to populate the grid is shown below:

$(document).ready(function(){
$.ajax(
    {
       type: "POST",
       url:'  /TestJSON/testAction.do?method=getJSONData'
,
       data: "",
       dataType: "json",
       success: function(result)
       {
            alert("success");
            alert("result: " + result.total);
            jQuery("#list10").jqGrid({
            url:'  /TestJSON/testAction.do?method=getJSONData',
                datatype: 'json',
                mtype: 'POST',
                colNames:['Invoice Number','Invoice Date','Invoice Name'],
         colModel:[
             {name:'id',index:'id', id:'name',width:90},
             {name:'invdate',index:'invdate', width:120},
             {name:'invname',index:'invname', width:200}
     ],
                pager: jQuery('#pager1'),
                rowNum: 5,
                rowList: [5, 10, 20, 50],
                viewrecords: true
            })
       },
       error: function(x, e)
       {
            alert("errorddd " + x.readyState + " "+ x.status +" "+ e);
            if (x.readyState == 4) {
             var response = x.responseText;
             alert(response);
            }
       }
    });
setTimeout(function() {$("#list10").jqGrid('setGridParam',{datatype:'json'}); },50);
});

--------------------------------------------------------------------------------------------------------------------------

 If any one faces any problem with the above mentioned implementation please post a reply to this topic.

Regards,

Aarati


Hi,

For populating the grid this code works fine. But i am unable to make the grid editable even by setting the editable:true option.

Can you please help resolve this issue?

Thanks,

Moin

02/07/2011
08:47
Avatar
mlotfi
Boston
New Member
Members
Forum Posts: 2
Member Since:
28/05/2010
sp_UserOfflineSmall Offline

Hi aarati,

I am new here, I will appreciate if you can send me the src code of a working example using :

1) Struts 1.2

2) JQGrid 3.8.2

3) JQuery 1.4.4

my email is : majidnakit@yahoo.com

Thanks lot.

03/07/2011
10:05
Avatar
aarati
Member
Members
Forum Posts: 8
Member Since:
05/01/2011
sp_UserOfflineSmall Offline

moin_mkhan said:

aarati said:

Hello,

Finally got it to work Smile.

Made slight modifications to the existing code:

Struts Action:

1) Earlier was returning to an empty jsp page with no specific code. Now returning null.

2) Using the write method of the jsonObj to write the response data.

The action method now looks like the following:

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



 JSONObject jsonObj = createJSONTestData();

System.out.println("Final JSON OBj: " + jsonObj.toString()); 

jsonObj.write(response.getWriter());

return null;
 }

 

--------------------------------------------------------------------------------------------------------------------------

JSP:

JSP Code to populate the grid is shown below:

$(document).ready(function(){
$.ajax(
    {
       type: "POST",
       url:'  /TestJSON/testAction.do?method=getJSONData'
,
       data: "",
       dataType: "json",
       success: function(result)
       {
            alert("success");
            alert("result: " + result.total);
            jQuery("#list10").jqGrid({
            url:'  /TestJSON/testAction.do?method=getJSONData',
                datatype: 'json',
                mtype: 'POST',
                colNames:['Invoice Number','Invoice Date','Invoice Name'],
         colModel:[
             {name:'id',index:'id', id:'name',width:90},
             {name:'invdate',index:'invdate', width:120},
             {name:'invname',index:'invname', width:200}
     ],
                pager: jQuery('#pager1'),
                rowNum: 5,
                rowList: [5, 10, 20, 50],
                viewrecords: true
            })
       },
       error: function(x, e)
       {
            alert("errorddd " + x.readyState + " "+ x.status +" "+ e);
            if (x.readyState == 4) {
             var response = x.responseText;
             alert(response);
            }
       }
    });
setTimeout(function() {$("#list10").jqGrid('setGridParam',{datatype:'json'}); },50);
});

--------------------------------------------------------------------------------------------------------------------------

 If any one faces any problem with the above mentioned implementation please post a reply to this topic.

Regards,

Aarati


Hi,

For populating the grid this code works fine. But i am unable to make the grid editable even by setting the editable:true option.

Can you please help resolve this issue?

Thanks,

Moin


Did you include the necessary js files required for making the jqgrid editable.

Regards,

Aarati

10/02/2012
14:42
Avatar
surendra
Noida
Member
Members
Forum Posts: 3
Member Since:
10/02/2012
sp_UserOfflineSmall Offline

kirangentlebreeze1987 said:

aarati said:

Hello,

Finally got it to work Smile.

Made slight modifications to the existing code:

Struts Action:

1) Earlier was returning to an empty jsp page with no specific code. Now returning null.

2) Using the write method of the jsonObj to write the response data.

The action method now looks like the following:

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



 JSONObject jsonObj = createJSONTestData();

System.out.println("Final JSON OBj: " + jsonObj.toString()); 

jsonObj.write(response.getWriter());

return null;
 }

 

--------------------------------------------------------------------------------------------------------------------------

JSP:

JSP Code to populate the grid is shown below:

$(document).ready(function(){
$.ajax(
    {
       type: "POST",
       url:'  /TestJSON/testAction.do?method=getJSONData'
,
       data: "",
       dataType: "json",
       success: function(result)
       {
            alert("success");
            alert("result: " + result.total);
            jQuery("#list10").jqGrid({
            url:'  /TestJSON/testAction.do?method=getJSONData',
                datatype: 'json',
                mtype: 'POST',
                colNames:['Invoice Number','Invoice Date','Invoice Name'],
         colModel:[
             {name:'id',index:'id', id:'name',width:90},
             {name:'invdate',index:'invdate', width:120},
             {name:'invname',index:'invname', width:200}
     ],
                pager: jQuery('#pager1'),
                rowNum: 5,
                rowList: [5, 10, 20, 50],
                viewrecords: true
            })
       },
       error: function(x, e)
       {
            alert("errorddd " + x.readyState + " "+ x.status +" "+ e);
            if (x.readyState == 4) {
             var response = x.responseText;
             alert(response);
            }
       }
    });
setTimeout(function() {$("#list10").jqGrid('setGridParam',{datatype:'json'}); },50);
});

--------------------------------------------------------------------------------------------------------------------------

 If any one faces any problem with the above mentioned implementation please post a reply to this topic.

Regards,

Aarati


i do have some problem populating a json object values to my grid 

can you help me???


10/02/2012
14:43
Avatar
surendra
Noida
Member
Members
Forum Posts: 3
Member Since:
10/02/2012
sp_UserOfflineSmall Offline

aarati said:

Hello,

I get an empty grid when trying to obtain a json object from struts action method.

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?

Awaiting for a clarification at the earliest. Thank you.

Regards,

Aarati


10/02/2012
14:47
Avatar
surendra
Noida
Member
Members
Forum Posts: 3
Member Since:
10/02/2012
sp_UserOfflineSmall Offline

hiii aarti would you explain how to use jquery , json with java & jsp in struts 1.2 ? Please clarrify your code and what is the use url an how to automaically create jquery grid in jsp.

26/06/2012
15:07
Avatar
rohan.gandhi
New Member
Members
Forum Posts: 1
Member Since:
26/06/2012
sp_UserOfflineSmall Offline

Thanks a zillion Arati! That works like magic! Cool

13/09/2012
05:30
Avatar
vsendhil
Sydney
Member
Members
Forum Posts: 3
Member Since:
13/09/2012
sp_UserOfflineSmall Offline

What are the java libraries (jar) required for json?

I tried the above with json-lib-2.2.2-jdk15.jar and I am getting "java.lang.NoClassDefFoundError: net.sf.ezmorph.Morpher" error.

13/09/2012
10:18
Avatar
vsendhil
Sydney
Member
Members
Forum Posts: 3
Member Since:
13/09/2012
sp_UserOfflineSmall Offline

aarati said:

Hello,

Finally got it to work Smile.

Made slight modifications to the existing code:

Struts Action:

1) Earlier was returning to an empty jsp page with no specific code. Now returning null.

2) Using the write method of the jsonObj to write the response data.

The action method now looks like the following:

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



 JSONObject jsonObj = createJSONTestData();

System.out.println("Final JSON OBj: " + jsonObj.toString()); 

jsonObj.write(response.getWriter());

return null;
 }

 

--------------------------------------------------------------------------------------------------------------------------

JSP:

JSP Code to populate the grid is shown below:

$(document).ready(function(){
$.ajax(
    {
       type: "POST",
       url:'  /TestJSON/testAction.do?method=getJSONData'
,
       data: "",
       dataType: "json",
       success: function(result)
       {
            alert("success");
            alert("result: " + result.total);
            jQuery("#list10").jqGrid({
            url:'  /TestJSON/testAction.do?method=getJSONData',
                datatype: 'json',
                mtype: 'POST',
                colNames:['Invoice Number','Invoice Date','Invoice Name'],
         colModel:[
             {name:'id',index:'id', id:'name',width:90},
             {name:'invdate',index:'invdate', width:120},
             {name:'invname',index:'invname', width:200}
     ],
                pager: jQuery('#pager1'),
                rowNum: 5,
                rowList: [5, 10, 20, 50],
                viewrecords: true
            })
       },
       error: function(x, e)
       {
            alert("errorddd " + x.readyState + " "+ x.status +" "+ e);
            if (x.readyState == 4) {
             var response = x.responseText;
             alert(response);
            }
       }
    });
setTimeout(function() {$("#list10").jqGrid('setGridParam',{datatype:'json'}); },50);
});

--------------------------------------------------------------------------------------------------------------------------

 If any one faces any problem with the above mentioned implementation please post a reply to this topic.

Regards,

Aarati


Hi Aarthi,

I tried this and I am getting the alerts but there is not table populated, could you please share the full source code (jsp and action) or let us know the steps to do this.  Thanks

13/09/2012
10:19
Avatar
vsendhil
Sydney
Member
Members
Forum Posts: 3
Member Since:
13/09/2012
sp_UserOfflineSmall Offline

vsendhil said:What are the java libraries (jar) required for json?

I tried the above with json-lib-2.2.2-jdk15.jar and I am getting "java.lang.NoClassDefFoundError: net.sf.ezmorph.Morpher" error.


I downloaded the JARs and fixed this

05/07/2014
09:47
Avatar
anees.iny
New Member
Members
Forum Posts: 1
Member Since:
05/07/2014
sp_UserOfflineSmall Offline

kirangentlebreeze1987 said

aarati said:
Hello,

Finally got it to work Smile.

Made slight modifications to the existing code:

Struts Action:

1) Earlier was returning to an empty jsp page with no specific code. Now returning null.

2) Using the write method of the jsonObj to write the response data.

The action method now looks like the following:

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

…
…
 JSONObject jsonObj = createJSONTestData();

System.out.println("Final JSON OBj: " + jsonObj.toString()); 

jsonObj.write(response.getWriter());

return null;
 }

 

--------------------------------------------------------------------------------------------------------------------------

JSP:

JSP Code to populate the grid is shown below:

$(document).ready(function(){
$.ajax(
    {
       type: "POST",
       url:'  /TestJSON/testAction.do?method=getJSONData'
,
       data: "",
       dataType: "json",
       success: function(result)
       {
            alert("success");
            alert("result: " + result.total);
            jQuery("#list10").jqGrid({
            url:'  /TestJSON/testAction.do?method=getJSONData',
                datatype: 'json',
                mtype: 'POST',
                colNames:['Invoice Number','Invoice Date','Invoice Name'],
         colModel:[
             {name:'id',index:'id', id:'name',width:90},
             {name:'invdate',index:'invdate', width:120},
             {name:'invname',index:'invname', width:200}
     ],
                pager: jQuery('#pager1'),
                rowNum: 5,
                rowList: [5, 10, 20, 50],
                viewrecords: true
            })
       },
       error: function(x, e)
       {
            alert("errorddd " + x.readyState + " "+ x.status +" "+ e);
            if (x.readyState == 4) {
             var response = x.responseText;
             alert(response);
            }
       }
    });
setTimeout(function() {$("#list10").jqGrid('setGridParam',{datatype:'json'}); },50);
});

--------------------------------------------------------------------------------------------------------------------------

 If any one faces any problem with the above mentioned implementation please post a reply to this topic.

Regards,

Aarati


i do have some problem populating a json object values to my grid 

can you help me???

Hi aarati,

Can u provide me full code which is mentioned above.I need it very urgently Pls reply fast.

Thanks,

anees.iny

Forum Timezone: Europe/Sofia

Most Users Ever Online: 715

Currently Online:
29 Guest(s)

Currently Browsing this Page:
1 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