Hibernate framework realizes paging demo, el expression
js submit form
function queryesPagesDoc() {
queryForm.submit();
}
The jar package required for el expression: jstl-1.2.jar, standard-1.1.2.jar.
Page introduction: <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
Front-end esPagesDoc.jsp page body content
<body>
<div align="center" style="width:auto">
<div>
<div >
<div></div>
<div>es_pages页面</div>
</div>
<form name="queryForm" method="post" action="<%=basePath%>EsPagesDocServlet" style="margin: 0px;">
<table id="filter" width="100%">
<tr>
<th width="60px">文档ID:</th>
<td width="120px">
<input type="text" name="docId" id="docId" class="inputline"
value=${
esPagesDocBean.docId}>
</td>
<td width="500px">
<button type="button" id="query" class="btn_07" onclick="queryesPagesDoc()">
<span>查询 </span>
</button>
</td>
</tr>
</table>
</form>
</div>
<div">
<form name="myForm" method="post" style="margin: 0px;">
<input type="hidden" name="listCodes" value=""/>
<table width="100%" align="center" border="0" cellpadding="0" cellspacing="1" id="tab">
<tr>
<th>
<input type="checkbox" name="checkbox" onclick="changeAll()"/>
</th>
<th>文档ID</th>
<th>影像名称</th>
<th>创建日期</th>
<th>创建时间</th>
<th width="220px;">操作</th>
</tr>
<c:forEach items="${esPagesDocList}" var="esPagesDoc" varStatus="status">
<tr>
<th>
<input type="checkbox" name="informRecord"/>
</th>
<td>${
esPagesDoc.docId}</td>
<td>${
esPagesDoc.pageName}</td>
<td>${
esPagesDoc.makeDate}</td>
<td> ${
esPagesDoc.makeTime} </td>
</tr>
</c:forEach>
</table>
<div>
<table width="100%">
<tr>
<td>
共${
page.totalCount}条记录,
每页${
page.pageSize}条,
当前第${
page.currentPage}/${
page.pageCount}页
<c:choose>
<c:when test="${page.pageCount==1}"></c:when>
<c:when test="${page.previous && page.next}">
<a href="<%=basePath%>EsPagesDocServlet?currentPage=1&&docId=${esPagesDocBean.docId}"
style="text-decoration: none;color: #006FD3">[首页]</a>
<a href="<%=basePath%>EsPagesDocServlet?currentPage=${page.currentPage-1}&docId=${esPagesDocBean.docId}"
style="text-decoration: none;color: #006FD3">[上一页]</a>
<a href="<%=basePath%>EsPagesDocServlet?currentPage=${page.currentPage+1}&docId=${esPagesDocBean.docId}"
style="text-decoration: none;color: #006FD3">[下一页]</a>
<a href="<%=basePath%>EsPagesDocServlet?currentPage=${page.pageCount}&docId=${esPagesDocBean.docId}"
style="text-decoration: none;color: #006FD3">[尾页]</a>
</c:when>
<c:when test="${page.previous}">
<a href="<%=basePath%>EsPagesDocServlet?currentPage=1&docId=${esPagesDocBean.docId}"
style="text-decoration: none;color: #006FD3">[首页]</a>
<a href="<%=basePath%>EsPagesDocServlet?currentPage=${page.currentPage-1}&docId=${esPagesDocBean.docId}"
style="text-decoration: none;color: #006FD3">[上一页]</a>
</c:when>
<c:when test="${page.next}">
<a href="<%=basePath%>EsPagesDocServlet?currentPage=${page.currentPage+1}&docId=${esPagesDocBean.docId}"
style="text-decoration: none;color: #006FD3">[下一页]</a>
<a href="<%=basePath%>EsPagesDocServlet?currentPage=${page.pageCount}&docId=${esPagesDocBean.docId}"
style="text-decoration: none;color: #006FD3">[尾页]</a>
</c:when>
</c:choose>
<%--超链接多个参数用&拼接--%>
</td>
</tr>
</table>
</div>
</form>
</div>
</div>
</body>
Backend page tools
package com.sunyard.insurance.bean;
import java.util.List;
public class Page {
/**
* 默认的每页显示数据条数
*/
private static int DEFAULT_PAGE_SIZE = 10;
/**
* 每页显示数据
*/
private int pageSize = DEFAULT_PAGE_SIZE;
/**
* 总的数据数
*/
private int totalCount;
/**
* 当前页数
*/
private int currentPage;
/**
* 总页数
*/
private int pageCount;
/**
* 是否存在上一页
*/
private Boolean previous;
/**
* 是否存在下一页
*/
private Boolean next;
/**
* 分页显示的数据集合
*/
@SuppressWarnings("unchecked")
private List data;
/**
* 是否存在上一页
* @return
*/
public Boolean getPrevious() {
return previous;
}
/**
* 获取当前第几页
* @return
*/
public int getCurrentPage() {
return currentPage;
}
/**
* 是否存在下一页
* @return
*/
public Boolean getNext() {
return next;
}
/**
* 默认构造函数
*/
public Page(){
}
public void setPageSize(int pageSize){
this.pageSize = pageSize;
}
public int getPageCount() {
return pageCount;
}
public int getTotalCount() {
return this.totalCount;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
/**
* 设置分页对象的属性值
* @param currentPage
* @param totalCount
*/
public void calculatePageInfo(int currentPage,int totalCount){
this.totalCount = totalCount;
this.currentPage = currentPage;
this.pageCount = this.getTotalPageCount();
this.previous = this.hasPreviousPage();
this.next = this.hasNextPage();
}
/**
* 计算总页数
*/
public int getTotalPageCount() {
if (totalCount % pageSize == 0)
return totalCount / pageSize;
else
return totalCount / pageSize + 1;
}
/**
* 设置每页数据条数
*/
public int getPageSize() {
return pageSize;
}
/**
* 判断是否存在下一页
*/
public boolean hasNextPage() {
return this.currentPage < this.pageCount ;
}
/**
* 判断是否存在上一页
*/
public boolean hasPreviousPage() {
return this.currentPage > 1;
}
protected int getStartOfPage(int pageNo) {
return getStartOfPage(pageNo, DEFAULT_PAGE_SIZE);
}
public int getStartOfPage(int pageNo, int pageSize) {
return (pageNo - 1) * pageSize;
}
@SuppressWarnings("unchecked")
public List getResult() {
return data;
}
/**
* 设置分页数据集合
* @param data
*/
@SuppressWarnings("unchecked")
public void setResult(List data) {
this.data = data;
}
/**
* 设置数据总条数
* @param totalCount
*/
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
}
Tool method
public class CommonUtil {
public static boolean isBlank(String str) {
int strLen;
if (str != null && (strLen = str.length()) != 0) {
for (int i = 0; i < strLen; ++i) {
if (!Character.isWhitespace(str.charAt(i))) {
return false;
}
}
return true;
} else {
return true;
}
}
public static boolean isNotBlank(String str) {
return !isBlank(str);
}
}
Backend servlet
public class EsPagesDocServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HandleFailedRecordService handleFailedRecordService = SpringBeanService.getBeans(HandleFailedRecordService.class);
HttpSession session = request.getSession();
Page page = new Page();
EsPagesDoc esPagesDoc = new EsPagesDoc();
String currentPage = request.getParameter("currentPage");
String docId = request.getParameter("docId");
esPagesDoc.setDocId(docId);
if(CommonUtil.isBlank(currentPage)){
currentPage="1";
}
long esPagesDocCount = handleFailedRecordService.getEsPagesDocCount(esPagesDoc);
page.calculatePageInfo(Integer.parseInt(currentPage),(int)esPagesDocCount);
List<EsPagesDoc> esMainDocList = handleFailedRecordService.getEsPagesDoc(esPagesDoc, page);
session.setAttribute("esPagesDocList", esMainDocList);
session.setAttribute("esPagesDocBean", esPagesDoc);
session.setAttribute("page", page);
request.getRequestDispatcher("/esPagesDoc.jsp").forward(request, response);
}
}
dao layer implementation class method (service and other methods omitted)
/**
* 查询数据总数,计算分页要用
*/
@Override
public long getEsPagesDocCount(EsPagesDoc esPagesDoc) {
String docId = esPagesDoc.getDocId();
String hql;
if (CommonUtil.isNotBlank(docId)){
hql = "select count(*) from EsPagesDoc where DOCID='"+docId+"'";
}else{
hql = "select count(*) from EsPagesDoc";
}
long count = 0;
try {
Query query = this.getSession().createQuery(hql);
count = (Long)query.uniqueResult();
} catch (Exception e) {
e.printStackTrace();
}
return count;
}
/**
* 以分页方式查询数据返回
*/
@Override
public List<EsPagesDoc> getEsPagesDoc(EsPagesDoc esPagesDoc, Page page) {
String docId = esPagesDoc.getDocId();
String hql;
if (CommonUtil.isNotBlank(docId)){
hql = "from EsPagesDoc where DOCID='"+docId+"'";
}else{
hql = "from EsPagesDoc";
}
List<EsPagesDoc> pageList = null;
try {
pageList = getPageList(hql, page);
} catch (Exception e) {
e.printStackTrace();
}
return pageList;
}
public <T> List<T> getPageList(String hql, Page page){
Query query=this.getSession().createQuery(hql);
//设置起始查询行数
query.setFirstResult(page.getStartOfPage(page.getCurrentPage()));
//设置每页显示的数据
query.setMaxResults(page.getPageSize());
return query.list();
}