Forum
07:04
05/01/2011
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
09:26
05/01/2011
Hello,
Finally got it to work .
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
12:14
20/03/2011
aarati said:
Hello,
Finally got it to work .
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???
07:39
26/04/2011
aarati said:
Hello,
Finally got it to work .
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
08:47
28/05/2010
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.
10:05
05/01/2011
moin_mkhan said:
aarati said:
Hello,
Finally got it to work .
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
14:42
10/02/2012
kirangentlebreeze1987 said:
aarati said:
Hello,
Finally got it to work .
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???
14:43
10/02/2012
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 objectJSONObject 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:18
13/09/2012
aarati said:
Hello,
Finally got it to work .
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
09:47
05/07/2014
kirangentlebreeze1987 said
aarati said:
Hello,Finally got it to work .
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
Most Users Ever Online: 715
Currently Online:
51 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.comModerators: tony: 7721, Rumen[Trirand]: 81
Administrators: admin: 66