所以我终于解决了我的分页jquery数据表问题。但随后出现了新的问题。它仍然加载1000行数据,而不是我想要的数据量。所以我认为它在服务器端抛出隐藏变量,例如 sEcho , iTotalRecords , iTotalDisplayRecords 等等。我做的是找到关于它的指南this。我研究了文章并试图将它整合到我的文章中。
以下是整合:
用于控制器
@RequestMapping(value = "populate/pull", method = RequestMethod.GET)
public void populatePull(HttpServletRequest request,
HttpServletResponse response) throws IOException {
JqueryDataTableModel param = DatatableParams.getParam(request);
String sEcho = param.sEcho;
int iTotalRecords=0; // total number of records (unfiltered)
int iTotalDisplayRecords; //value will be set when code filters companies by keyword
List<KspeakPull> pullList = pullServive.viewAllPull();
System.out.println("Viewing all");
iTotalDisplayRecords=pullList.size();
if(pullList.size()< param.iDisplayStart + param.iDisplayLength) {
pullList = pullList.subList(param.iDisplayStart, pullList.size());
} else {
pullList = pullList.subList(param.iDisplayStart, param.iDisplayStart + param.iDisplayLength);
}
Gson gson = new Gson();
JsonObject jsonResponse = new JsonObject();
jsonResponse.addProperty("sEcho", sEcho);
jsonResponse.addProperty("iTotalRecords", iTotalRecords);
jsonResponse.addProperty("iTotalDisplayRecords", iTotalDisplayRecords);
jsonResponse.add("aaData", gson.toJsonTree(pullList));
System.out.println(jsonResponse.toString());
response.setContentType("application/Json");
response.getWriter().print(jsonResponse.toString());
}
jQuery表模型从指南中复制了这个,我想知道这是不是因为他没有使用任何getter / setter:
public class JqueryDataTableModel {
// / Request sequence number sent by DataTable, same value must be returned
// in response
public String sEcho;
// / Text used for filtering
public String sSearch;
// / Number of records that should be shown in table
public int iDisplayLength;
// / First record that should be shown(used for paging)
public int iDisplayStart;
// / Number of columns in table
public int iColumns;
// / Number of columns that are used in sorting
public int iSortingCols;
// / Index of the column that is used for sorting
public int iSortColumnIndex;
// / Sorting direction "asc" or "desc"
public String sSortDirection;
// / Comma separated list of column names
public String sColumns;
}
和数据表参数:
public class DatatableParams {
public static JqueryDataTableModel getParam(HttpServletRequest request) {
if (request.getParameter("sEcho") != null
&& request.getParameter("sEcho") != "") {
JqueryDataTableModel param = new JqueryDataTableModel();
param.sEcho = request.getParameter("sEcho");
param.sSearch = request.getParameter("sSearch");
param.sColumns = request.getParameter("sColumns");
param.iDisplayStart = Integer.parseInt(request
.getParameter("iDisplayStart"));
param.iDisplayLength = Integer.parseInt(request
.getParameter("iDisplayLength"));
param.iColumns = Integer.parseInt(request.getParameter("iColumns"));
param.iSortingCols = Integer.parseInt(request
.getParameter("iSortingCols"));
param.iSortColumnIndex = Integer.parseInt(request
.getParameter("iSortCol_0"));
param.sSortDirection = request.getParameter("sSortDir_0");
return param;
} else
return null;
}
}
在我的JSP中导入以下脚本:
<script src="<c:url value='/resources/jquery-1.8.3.js'/>"></script>
<script src="<c:url value='/resources/bootstrap/js/jquery.dataTables.min.js'/>"></script>
<script src="<c:url value='/resources/bootstrap/js/pull-populate.js' />"></script>
<script src="<c:url value='/resources/bootstrap/js/bootstrap.min.js' />"></script>
和我的ajax代码:
$(document).ready(function() {
$("#tablediv").hide();
$("#showTable").click(function(event){
$.get('populate/pull',function(responseJson) {
if(responseJson!=null){
$("#pulltable").DataTable({
"bServerSide": true,
"sAjaxSource": "populate/pull",
"bProcessing": true,
"sPaginationType": "full_numbers",
"bJQueryUI": true,
"aoColumns": [
{ "mDataProp": "id" },
{ "mDataProp": "alias1" },
{ "mDataProp": "alias2" },
{ "mDataProp": "alias3" },
{ "mDataProp": "alias4" },
{ "mDataProp": "keyword" },
{ "mDataProp": "charNo" },
{ "mDataProp": "korWord" },
{ "mDataProp": "korCharNo" },
{ "mDataProp": "charTotal" },
] });
}
});
$("#tablediv").show();
}); });
运行时会在此行中生成NullPointerException
:
String sEcho = param.sEcho;
那么,我在这里错过了什么?显然它没有收到请求。
答案 0 :(得分:1)
&& request.getParameter("sEcho") != ""
您正在使用!=
来检查引用相等性,而不是值相等(请参阅How do I compare strings in Java?)。
由于这将是false
,您的else
将始终返回null
。然后,您执行String sEcho = param.sEcho;
,param
将null
。
答案 1 :(得分:0)
&& !request.getParameter("sEcho").equals("")
这应该比较值,并且比使用!=
更好。 Equals
比较这些值,你不应该再遇到这种问题了。