Reputation: 133
I am trying to paginate rows of a table inside my servlet using hibernate.But once I click on the desire index of the page it always gives me only the first set of row of the table.
My servlet code:
int pageIndex = 0;
int totalNumberOfRecords = 0;
int numberOfRecordsPerPage = 5;
String sPageIndex = request.getParameter("pageIndex");
if (sPageIndex == null) {
pageIndex = 1;
} else {
pageIndex = 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...}
List<ProductHasSize> phs1 = ses.createCriteria(ProductHasSize.class)
.setProjection(Projections.rowCount()).list();
Iterator i = phs1.iterator();
if (i.hasNext()) {
Object o = i.next();
totalNumberOfRecords = Integer.parseInt(o.toString());
}
int noOfPages = totalNumberOfRecords / numberOfRecordsPerPage;
if (totalNumberOfRecords > (noOfPages * numberOfRecordsPerPage)) {
noOfPages = noOfPages + 1;
}
for (int j = 1; j <= noOfPages; j++) {
String myurl = "products.jsp?pageIndex=" + j;
String active = j == pageIndex ? "active" : "";
pagination = pagination + "<li class='" + active + "'><a href=" + myurl + ">" + j + "</a></li>";
}
Thanks in advance.
Upvotes: 1
Views: 928
Reputation: 8387
Try to replace:
if (i.hasNext()) {
With:
while(i.hasNext()) {
And write the while loop like this:
while(i.hasNext()) {
Object o = i.next();
totalNumberOfRecords += Integer.parseInt(o.toString());
int noOfPages = totalNumberOfRecords / numberOfRecordsPerPage;
if (totalNumberOfRecords > (noOfPages * numberOfRecordsPerPage)) {
noOfPages = noOfPages + 1;
}
}
Upvotes: 1