Я пытаюсь разбивать строки на таблицу внутри моего сервлета, используя hibernate. Но как только я нажимаю на индекс желания страницы, он всегда дает мне только первый набор строк таблицы. Поэтому я положил System.out.print()
на все основные разделы и, наконец, узнал, что request.getParameter("pgIndex")
всегда возвращается null.request.getParameter ("pgIndex") всегда возвращает null в сервлет
Мой сервлет код:
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');
}
});
});
UPDATE:
$(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');
}
});
});
Спасибо заранее.
product.jsp doen's handle pgIndex и pgIndex никогда не передаются в ваш сервлет (если сервлет связан с вашим вызовом ajax ?!) – Jan
@Jan Что вы подразумеваете под **, если сервлет связан с вашим вызовом ajax * *? 'url: 'AdimnProductFilterAction','. AdimnProductFilterAction - мое имя сервлета. поэтому я связал это. –
Вот что я догадался. Вы никогда не передаете параметр в этот сервлет. Нет? PgIndex = и никакие данные не отправляются как сообщение – Jan