request.getParameter(“pgIndex”)始终在servlet中返回null

时间:2016-01-15 05:01:45

标签: javascript java hibernate jsp servlets

我正在尝试使用hibernate.tut在我的servlet中对表的行进行分页。一旦我点击页面的欲望索引,它总是只给我表的第一组行。所以我放{{1}在每个主要部分,最后发现 System.out.print() 始终返回 null

我的servlet代码:

request.getParameter("pgIndex")

products.jsp

    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 + "\"}]");

的JavaScript

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

更新:

$(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');
                    }
                });

            });

提前致谢。

1 个答案:

答案 0 :(得分:2)

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

发送为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调用中。