我在这里错过了什么?

What am I Missing here?

所以我终于解决了我的分页jquery数据table问题。但随后又出现了新的问题。它仍然加载 1000 行数据,它没有遵循我想要的数据数量。所以我认为它在服务器端抛出隐藏变量,例如 sEchoiTotalRecordsiTotalDisplayRecords等等。我所做的是找到一个关于它的指南 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 table 模型从指南中复制了这个,我想知道是否是这个问题,因为他没有使用任何 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;
}

和数据table参数:

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 将始终 return null。然后,您执行 String sEcho = param.sEcho;param 将是 null

&& !request.getParameter("sEcho").equals("")    

这应该比较值并且是比使用 != 更好的做法。 Equals 比较值,你应该不会再有那种问题了。