因此,我终于解决了分页jquery datatable问题。但是随后出现了新的问题。它仍然加载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表格模型从指南中复制了此模型,我想知道这是否是问题所在,因为他没有使用任何吸气剂/塞特剂:
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;
那么,我在这里想念什么?显然,它没有收到请求。
最佳答案
&& request.getParameter("sEcho") != ""
您正在使用
!=
来检查引用的相等性,而不是值的相等性(请参见How do I compare strings in Java?)。由于这将是
false
,因此您的else
将始终返回null
。然后执行String sEcho = param.sEcho;
,param
将为null
。