我是jQuery的新手,發現從我的servlet到我的jsp中的jqGrid的數據顯示有困難。 我已經使用谷歌gson將數據從ArrayList轉換爲字符串變量json。 當我運行項目並顯示一個空網格時,它在控制檯中顯示json數據。從servlet到jqGrid的JSON數據不顯示
Student.java
package com
public class Student {
private String name;
private String mark;
private String address;
//getter and setters
StudentDataService.java
package com;
import java.util.ArrayList;
import java.util.List;
import com.Student;
public class StudentDataService {
public static List<Student> getStudentList() {
List<Student> listOfStudent = new ArrayList<Student>();
Student aStudent = new Student();
for (int i = 1; i <= 10; i++) {
aStudent = new Student();
aStudent.setName("R" + i);
aStudent.setMark("20" + i);
aStudent.setAddress("pune "+i);
listOfStudent.add(aStudent);
}
return listOfStudent;
}
}
我的servlet代碼:
StudentDataServlet.java
package com;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.Student;
import com.StudentDataService;
/**
* Servlet implementation class StudentDataServlet
*/
public class StudentDataServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public StudentDataServlet() {
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/json");
PrintWriter out = response.getWriter();
List<Student> lisOfStudent = StudentDataService.getStudentList();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String json = gson.toJson(lisOfStudent);
out.print(json);
System.out.println(json);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
我的JSP頁面:
slickGridDemo.jsp
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>jqGrid Example</title>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.6.2.js'></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/
libs/jqueryui/1.8.14/jquery-ui.js">
</script>
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/themes/base/jquery-ui.css">
<link rel="stylesheet" type="text/css" href="http://trirand.com/blog/jqgrid/themes/ui.jqgrid.css">
<script type='text/javascript' src="http://trirand.com/blog/jqgrid/js/i18n/grid.locale- en.js"></script>
<script type='text/javascript' src="http://trirand.com/blog/jqgrid/js/jquery.jqGrid.min.js"></script>
<style type='text/css'>
</style>
<script type='text/javascript'>
jQuery(document).ready(function() {
jQuery("#grid").jqGrid({
url: "http://localhost:9080/JquerySlickGrid/StudentDataServlet",
datatype: "json",
jsonReader: {repeatitems: false, id: "ref"},
colNames:['Name','Marks', 'Address'],
colModel:[
{name:'Name',index:'Name', width:100},
{name:'Marks',index:'Marks', width:100},
{name:'Address',index:'Address', width:500}
],
rowNum:20,
rowList:[20,60,100],
height:460,
pager: "#pagingDiv",
viewrecords: true,
caption: "Json Example"
});
});
</script>
</head>
<body>
<table id="grid"></table>
<div id="pagingDiv"></div>
</body>
</html>
仍無法得到關於網格值雖然它顯示控制檯,當我打印出來。 var newdata = jQuery.parseJSON(data) 它是作爲「數據」傳遞的json字符串嗎? 您是從某些struts DispatchAction方法打印值嗎?因爲我是struts的新手,所以這個URL看起來很熟悉。 – Roikka
是的,我正在使用struts。你可以發佈你的json數據嗎?沒有數據診斷變得很難.. –
我已經更新了我的答案。我正在使用json數組和POJO類(ReportDto)。如果您想使用Json數據,請正確配置您的json map reader。使用以下鏈接。 [檢索數據](http://www.trirand.com/jqgridwiki/doku.php?ID =維基:retrieving_data) –