【发布时间】:2016-01-15 05:01:44
【问题描述】:
我正在尝试使用 hibernate 在我的 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 + "\"}]");
products.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');
}
});
});
提前致谢。
【问题讨论】:
-
products.jsp 的句柄 pgIndex 和 pgIndex 永远不会传递到您的 servlet(如果 servlet 链接到您的 ajax 调用?!)
-
@Jan 如果 servlet 链接到您的 ajax 调用 是什么意思?
url: 'AdimnProductFilterAction',。 AdimnProductFilterAction 是我的 servlet 名称。所以我已经链接了它。 -
这就是我的猜测。您永远不会将参数传递给该 servlet。没有 ?pgIndex= 也没有数据作为 post 发送
-
@Jan 那么
String myurl = "products.jsp?pgIndex=" + j; String active = j == pageIndex ? "active" : ""; s2 = s2 + "<li class='" + active + "'><a href=" + myurl + ">" + j + "</a></li>";呢?不是传入servlet吗??? -
但这在 servlet 内部生成代码来调用 product.jsp - 该参数似乎未使用
标签: javascript java hibernate jsp servlets