request.getParameter(“pgIndex”) 总是在 servlet 中返回 null

request.getParameter("pgIndex") always returns null in the servlet

本文关键字:servlet null 返回 getParameter pgIndex request      更新时间:2023-09-26

我正在尝试使用休眠对 servlet 中的表行进行分页。但是一旦我点击页面的欲望索引,它总是只给我表格的第一行。所以我在每个主要部分都放了System.out.print(),最后发现request.getParameter("pgIndex")总是返回 null

我的servlet代码:

    int pageIndex = 0;
    int totalNumberOfRecords = 0;
    int numberOfRecordsPerPage = 5;
    String sPageIndex = request.getParameter("pgIndex");
 //whether pgIndex=1 or pgIndex=2 in the url, always returns null as the output.
    System.out.println("pg - " + sPageIndex);
    pageIndex = sPageIndex == null ? 1 : Integer.parseInt(sPageIndex);
    int s = (pageIndex * numberOfRecordsPerPage) - numberOfRecordsPerPage;

List<ProductHasSize> phs = ses.createCriteria(ProductHasSize.class)
                .setFirstResult(s)
                .setMaxResults(numberOfRecordsPerPage)
                .list();
        for (ProductHasSize pro : phs) {... some html content here...}

      Criteria criteriaCount = ses.createCriteria(ProductHasSize.class);
        criteriaCount.setProjection(Projections.rowCount());
        totalNumberOfRecords = (int) (long) (Long) criteriaCount.uniqueResult();
        int noOfPages = totalNumberOfRecords / numberOfRecordsPerPage;
        if (totalNumberOfRecords > (noOfPages * numberOfRecordsPerPage)) {
            noOfPages = noOfPages + 1;
        }
        for (int j = 1; j <= noOfPages; j++) {
            String myurl = "products.jsp?pgIndex=" + j;
            String active = j == pageIndex ? "active" : "";
            s2 = s2 + "<li class='" + active + "'><a href=" + myurl + ">" + j + "</a></li>";
        }
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write("[{'"d1'":'"" + s1 + "'",'"d2'":'"" + s2 + "'"}]");

产品.jsp

<div class="row">
      <div class="col-md-12">
           <ul class="pagination" id="pagId"></ul>
       </div>
</div>

JavaScript

$(document).ready(function () {
                $.ajax({
                    url: 'AdimnProductFilterAction',
                    dataType: 'json',
                    cache: false,
                    success: function (data) {
                        $.each(data, function (key, value) {
                            $('#proFilterTab').html(value.d1);
                            $('#pagId').html(value.d2);
                        });
                    },
                    error: function () {
                        alert('error');
                    }
                });
            });

更新:

 $(document).on("click", "#pagId a", function (event) {
    //tried with adding another function . But still returns null.
    event.preventDefault();
    var para = $(this).attr('href').match(/'d+/);
    $.ajax({
        url: 'AdimnProductFilterAction',
        dataType: 'json',
        data: {pgIndex: para},
        cache: false,
        success: function (data) {
            $.each(data, function (key, value) {
                $('#proFilterTab').html(value.d1);
                $('#pagId').html(value.d2);
            });
        },
        error: function () {
            alert('error');
        }
    });
});

提前谢谢。

在发送 JSON 数据时,您不会简单地将其作为请求参数接收。相反,只需添加"normal"参数:

以 HTTP POST 发送

$.ajax({
    url: 'AdimnProductFilterAction',
    type: 'POST',
    data: {
       'pgIndex': para
    },
    cache: false,
    success: function (data) {
        $.each(data, function (key, value) {
            $('#proFilterTab').html(value.d1);
            $('#pagId').html(value.d2);
        });
    },
    error: function () {
        alert('error');
    }
});

或作为 HTTP GET

$.ajax({
    url: 'AdimnProductFilterAction?pgIndex='+para,
    cache: false,
    success: function (data) {
        $.each(data, function (key, value) {
            $('#proFilterTab').html(value.d1);
            $('#pagId').html(value.d2);
        });
    },
    error: function () {
        alert('error');
    }
});

将参数添加到 servlet 调用中。