This article describes the method of Struts2+Hibernate to implement data paging. Share it for your reference, as follows:
1. Use Hibernate to implement paging technology:
/*** Use hql statement for page query* @param hql hql statement that needs to be query* @param offset first record index* @param pageSize Number of records to be displayed per page* @return All records of the current page*/@SuppressWarnings("unchecked")public List findByPage(final String hql,final int offset, final int pageSize){ //Execute query through a HibernateCallback object List list = getHibernateTemplate() .executeFind(new HibernateCallback() { //Methods that must be implemented in the HibernateCallback interface public Object doInHibernate(Session session) throws HibernateException, SQLException { //Execute Hibernate pagination query List result = session.createQuery(hql) .setFirstResult(offset) .setMaxResults(pageSize) .list(); return result; } }); return list;}// Get the total number of records public int getRows(String hql) { return getHibernateTemplate().find(hql).size();}2. Call Hibernate to implement paging technology in Action and jump to the display interface:
// Paging @SuppressWarnings("unchecked")public String paging() { String hql = "from Income"; // Paging data table int pageSize = 3; // Number of records displayed per page int allRows = service.getRows(hql); // Total records int allPage = 0; // Total pages int offset = getPage() + 1; // Index of the first record/*if (rows % size != 0) { pageSize = rows / size + 1; } else { pageSize = rows / size; }*/ allPage = (allRows - 1) / pageSize + 1; // Calculate the total number of pages List<Income> income = service.findByPage(hql, (offset-1)*pageSize, pageSize); request.setAttribute("allPage", allPage); request.setAttribute("offset", offset); request.setAttribute("income", income); return "paging";}3.struts.xml configuration:
<action name="income" > <!-- Configure view page for two logical views --> <result name="error">/error.jsp</result> <result name="paging">/income/income_list.jsp</result> <result name="update">/income/income_edit.jsp</result>
4. Display interface income_list.jsp
<%@ page language="java" pageEncoding="GBK"%><%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %><%@ taglib prefix="s" uri="/struts-tags" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><head> <title>Revenue List</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <link rel="stylesheet" type="text/css" href="../images/styles.css"></head><body> <div> <table cellpadding="0" cellpacing="0" align="center"> <tr> <td> ・Current location: Revenue Management>>View revenue</td> </tr> <tr> <td bgcolor="#FFFFFF"> <br> <table align="center" cellpadding="1" cellpacing="1" bgcolor="#036500" bordercolor="#FFFFF"> <tr bgcolor="#FFFFF"> <td align="center"> Income number</td> <td align="center"> Date</td> <td align="center"> Method</td> <td align="center"> Amount</td> <td align="center"> Project</td> <td align="center"> Source</td> <td align="center"> align="center"> Personnel</td> <td align="center"> Note</td> <td align="center"> Operation</td> </tr> <s:iterator value="#request.income"> <tr bgcolor="#FFFFFF"> <td align="center"><s:property value="id"/></td> <td align="center"><s:date name="date" format="yyyy-MM-dd"/></td> <td align="center"><s:property value="money"/></td> <td align="center"><s:property value="project"/></td> <td align="center"><s:property value="source"/></td> <td align="center"><s:property value="personnel"/></td> <td align="center"><s:property value="remarks"/></td> <td align="center"><s:property value="remarks"/></td> <td align="center"> <a href="javascript:if(confirm('Are you sure you want to delete ${id}?'))location='income!del?id=${id}'">Delete</a> <a href="javascript:if(confirm('Are you sure you want to modify ${id}?'))location='income!updateTo?id=${id}'">Modify</a> </td> </tr> </s:iterator> </table> <center> There are a total of ${allPage} pages, currently the ${offset} page<a href="income!paging?page=0"><font size="2" color="blue">Home</font></a> <a href="javascript:if(${offset}>1)location='income!paging?page=${page-1}'"><font size="2" color="red">Previous page</font></a> <a href="javascript:if(${offset}<${allPage})location='income!paging?page=${page+1}'"><font size="2" color="red">Next page</font></a> <a href="income!paging?page=${allPage-1}"><font size="2" color="blue">Last page</font></a> </td> </tr> </table> </div></body>5. Pagination results:
This article does not provide implementations in the underlying database, but as long as you master the paging principle, I believe this is not a big problem. For specific paging principles, please refer to the previous article: "Analysis of Hibernate Framework Data Paging Technology Examples"
I hope that the description in this article will be helpful to everyone's Java programming based on the Hibernate framework.