Você está na página 1de 4

jqGrid not being populated with JSON data from Java Servlet

@Override
protected void doGet(HttpServletRequest aRequest,
HttpServletResponse aResponse, Hashtable aQueryData,
LocaleData aLocale) throws ServletException, IOException {
System.out.println("doGet(): Received Request: " + aRequest.getServletPa
th());
// jqGrid expects the JSON data in a predefined format:
//
{
//
"total": "xxx",
//
"page": "yyy",
//
"records": "zzz",
//
"rows" : [
//
{"id" :"1", "cell" :["cell11", "cell12", "cell13"]},
//
{"id" :"2", "cell":["cell21", "cell22", "cell23"]},
//
...
//
]
//
}
// Calling getLogEntries() method populates logEntries & logEntriesCnt.
// logEntries contains the "rows" data as specified above.
// For now I am testing with 10 rows of data.
getLogEntries(aLocale);
JSONObject jqGridData = new JSONObject();
jqGridData.put("total", "1");
jqGridData.put("page", "1");
jqGridData.put("records", String.valueOf(logEntriesCnt-1));
jqGridData.put("rows", logEntries);
System.out.println("nn# Event Log Entries (" + new Date() + "):" + (logE
ntriesCnt-1));
System.out.println("jqGrid JSON: n" + jqGridData.toJSONString());
aRequest.setAttribute("userdata", jqGridData.toJSONString());
aRequest.getRequestDispatcher("/jsp/eventlogtest.jsp").forward(aRequest,
aResponse);
}
# Event Log Entries (Fri Dec 09 11:02:25 GMT 2011):10
jqGrid JSON:
{"total":"1","page":"1","records":"10","rows":[{"id":"1","cell":["09/12/11","11:
01:52","Communication Established"]},{"id":"2","cell":["09/12/11","11:01:52","Mo
nitoring Started"]},{"id":"3","cell":["09/12/11","10:50:55","Communication Estab
lished"]},{"id":"4","cell":["09/12/11","10:50:55","Monitoring Started"]},{"id":"
5","cell":["09/12/11","10:36:57","Communication Established"]},{"id":"6","cell":
["09/12/11","10:36:57","Monitoring Started"]},{"id":"7","cell":["09/12/11","10:3
0:58","Communication Established"]},{"id":"8","cell":["09/12/11","10:30:58","Mon
itoring Started"]},{"id":"9","cell":["09/12/11","10:21:58","Communication Establ
ished"]},{"id":"10","cell":["09/12/11","10:21:58","Monitoring Started"]}]}
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.or
g/TR/html4/loose.dtd">
<html>
<head>

<meta http-equiv="Content-Type" content="application/json">


<link type="text/css" rel="stylesheet" href="/styles/cupertino/jquery-ui-1.8.1
4.custom.min.css">
<link type="text/css" rel="stylesheet" href="/styles/ui.jqgrid.css">
<script type="text/javascript" src="/js/jquery-1.5.2.min.js"></script>
<script type="text/javascript" src="/js/jquery-ui-1.8.14.custom.min.js"></scri
pt>
<script type="text/javascript" src="/js/grid.locale-en.js"></script>
<script type="text/javascript" src="/js/jquery.jqGrid.min.js"></script>
<script type="text/javascript" src="/js/eventlog.min.js"></script>
<title>jqGrid Test</title>
</head>
<body>
<h1>jqGrid Test</h1>
<form id="formpageform" action="/eventlog" name="eventlogviewerform" method=
"post">
<div id="logEntries">
userdata = ${userdata}
</div>
<br/>
<br/>
<table id="tableGrid"></table>
<div id="tablePager"></div>
<br/>
<br/>
</form>
</body>
</html>
_search false
nd 1323429509833
page
1
rows
10
sidx
dateentry
sord
asc
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.or
g/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="application/json">
<link type="text/css" rel="stylesheet" href="/styles/cupertino/jquery-ui-1.8.1
4.custom.min.css">
<link type="text/css" rel="stylesheet" href="/styles/ui.jqgrid.css">
<script type="text/javascript" src="/js/jquery-1.5.2.min.js"></script>
<script type="text/javascript" src="/js/jquery-ui-1.8.14.custom.min.js"></scri

pt>
<script type="text/javascript" src="/js/grid.locale-en.js"></script>
<script type="text/javascript" src="/js/jquery.jqGrid.min.js"></script>
<script type="text/javascript" src="/js/eventlog.min.js"></script>
<title>jqGrid Test</title>
</head>
<body>
<h1>jqGrid Test</h1>
<form id="formpageform" action="/eventlog" name="eventlogviewerform" method=
"post">
<div id="logEntries">
userdata = {"total":"1","page":"1","records":"10","rows":[{"id":"1",
"cell":["09/12/11","11:18:13","Communication Established"]},{"id":"2","cell":["0
9/12/11","11:18:13","Monitoring Started"]},{"id":"3","cell":["09/12/11","11:01:5
2","Communication Established"]},{"id":"4","cell":["09/12/11","11:01:52","Monito
ring Started"]},{"id":"5","cell":["09/12/11","10:50:55","Communication Establish
ed"]},{"id":"6","cell":["09/12/11","10:50:55","Monitoring Started"]},{"id":"7","
cell":["09/12/11","10:36:57","Communication Established"]},{"id":"8","cell":["09
/12/11","10:36:57","Monitoring Started"]},{"id":"9","cell":["09/12/11","10:30:58
","Communication Established"]},{"id":"10","cell":["09/12/11","10:30:58","Monito
ring Started"]}]}
</div>
<br/>
<br/>
<table id="tableGrid"></table>
<div id="tablePager"></div>
<br/>
<br/>
</form>
</body>
</html>
jqGrid Test
userdata = {"total":"1","page":"1","records":"10","rows":[{"id":"1","cell":["09/
12/11","11:18:13","Communication Established"]},{"id":"2","cell":["09/12/11","11
:18:13","Monitoring Started"]},{"id":"3","cell":["09/12/11","11:01:52","Communic
ation Established"]},{"id":"4","cell":["09/12/11","11:01:52","Monitoring Started
"]},{"id":"5","cell":["09/12/11","10:50:55","Communication Established"]},{"id":
"6","cell":["09/12/11","10:50:55","Monitoring Started"]},{"id":"7","cell":["09/1
2/11","10:36:57","Communication Established"]},{"id":"8","cell":["09/12/11","10:
36:57","Monitoring Started"]},{"id":"9","cell":["09/12/11","10:30:58","Communica
tion Established"]},{"id":"10","cell":["09/12/11","10:30:58","Monitoring Started
"]}]}
<div id="logEntries">
userdata = ${userdata}
</div>
<div id="logEntries">
userdata = ${userdata}
</div>

<div id="logEntries">
userdata = {"total":"1","page":"1",...}
</div>
<script type="text/javascript">
var mygriddata = {"total":"1","page":"1",...};
</script>
var mygriddata = '{"total":"1","page":"1",...}';
<script type="text/javascript">
var mygriddata = {
total: "1",
page: "1"//, ...
};
</script>
<div id="logEntries">
userdata = '${userdata}'
</div>
aResponse.setContentType("application/json");

Você também pode gostar