[英]What am I Missing here?
So I finally solved my pagination jquery datatable problem. 因此,我终于解决了分页jquery datatable问题。 But then new problems arised.
但是随后出现了新的问题。 It still loads the 1000 rows of data it's not following the number of data that I wanted.
它仍然加载1000行数据,而不是跟随我想要的数据数量。 So I figured that it's throwing hidden variables on the server side such as the sEcho , iTotalRecords , iTotalDisplayRecords and etc.. What I did was to find a guide about it which is this .
所以我想,它是在服务器端抛出隐藏变量,如sEcho,iTotalRecords,iTotalDisplayRecords和等。我所做的就是找到一个关于它的指南,是这个 。 I studied article and tried to integrate it to mine.
我研究了文章,并试图将其整合到我的文章中。
Here's the integration: 这是集成:
for the controller 对于控制器
@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());
}
The jQuery table model copied this one from the guide, I'm wondering if this is the problem because he didn't use any getter/setter: 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;
}
and the datatable params: 和数据表参数:
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;
}
}
Imported the following scripts in my JSP: 在我的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>
and my ajax codes: 和我的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();
}); });
When running it causes a NullPointerException
on this line: 运行时会在此行上引起
NullPointerException
:
String sEcho = param.sEcho;
So, What am I missing here? 那么,我在这里想念什么? Obviously it's not receiving the request.
显然,它没有收到请求。
&& request.getParameter("sEcho") != ""
You are using !=
which will check reference equality, rather than value equality (see How do I compare strings in Java? ). 您正在使用
!=
,它将检查引用相等性,而不是值相等性(请参阅如何比较Java中的字符串? )。
Since this will be false
, your else
will always return null
. 由于这将是
false
,因此您的else
将始终返回null
。 You then do String sEcho = param.sEcho;
然后执行
String sEcho = param.sEcho;
, and param
will be null
. ,并且
param
将为null
。
&& !request.getParameter("sEcho").equals("")
This should compare the value and is a better practice than using !=
. 这应该比较该值,并且比使用
!=
更好。 Equals
compares the values and you should not have that kind of problem anymore. Equals
比较值,您应该再也不会有这种问题了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.