Pagination should be a work that we often do when developing web applications. It is very important to be able to implement pagination of databases and view layers in a relatively simple way.
In the database layer, use hibernate to paginate the database, and encapsulate the data queried from the database into Javabeans; in the view layer, paging can be easily implemented.
Create PageBean
package com.fishing.common.bean; import java.util.List; @SuppressWarnings("unchecked") public class PageBean { private List list; // list of records to be returned private int allRow; // Total records private int totalPage; // Total pages private int currentPage; // Current page private int pageSize; // Number of records per page private boolean isFirstPage; // Whether it is the first page private boolean isLastPage; // Whether it is the last page private boolean hasPreviousPage; // Is there a previous page private boolean hasNextPage; // Is there a next page public List getList() { return list; } public void setList(List list) { this.list = list; } public int getAllRow() { return allRow; } public void setAllRow(int allRow) { this.allRow = allRow; } public int getTotalPage() { return totalPage; } public void setTotalPage(int totalPage) { this.totalPage = totalPage; } public int getCurrentPage() { return currentPage; } public void setCurrentPage(int currentPage) { this.currentPage = currentPage; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } /** */ /** * Initialization paging information*/ public void init() { this.isFirstPage = isFirstPage(); this.isLastPage = isLastPage(); this.hasPreviousPage = isHasPreviousPage(); this.hasNextPage = isHasNextPage(); } /** */ /** * The following to judge the information of the page, just getter method (is method) * * @return */ public boolean isFirstPage() { return (currentPage == 1);// If the current page is the first page} public boolean isLastPage() { return currentPage == totalPage; //If the current page is the last page} public boolean isHasPreviousPage() { return currentPage != 1; //As long as the current page is not the first page} public boolean isHasNextPage() { return currentPage != totalPage; //As long as the current page is not the last page} /** */ /** * Calculate the total number of pages, a static method, and is directly called by the outside through the class name* * @param pageSize * Number of records per page* @param allRow * Total records* @return Total pages*/ public static int countTotalPage(final int pageSize, final int allRow) { int totalPage = allRow % pageSize == 0 ? allRow / pageSize : allRow / pageSize + 1; return totalPage; } /** */ /** * Calculate the start record of the current page* * @param pageSize * Number of records per page* @param currentPage * Current page* @return Current page start record number*/ public static int countOffset(final int pageSize, final int currentPage) { final int offset = pageSize * (currentPage - 1); return offset; } /** * / /** * Calculate the current page. If it is 0 or there is no "?page=" in the requested URL, then 1 is used instead * * @param page * The passed parameter (maybe empty, that is, 0, then 1 will be returned) * @return Current page*/ public static int countCurrentPage(int page) { final int curPage = (page == 0 ? 1 : page); return curPage; } } Add methods to Dao's abstract interface BaseDao:
public List queryForPage(final String hql, final int offset, final int length); Implement method in Dao's implementation class JianSheDWDaoImpl public List queryForPage(final String hql, final int offset, final int length) { List list = getHibernateTemplate().executeFind(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException, SQLException { Query query = session.createQuery(hql); query.setFirstResult(offset); query.setMaxResults(length); List list = query.list(); return list; } }); return list; } Add methods to the service abstraction layer interface JianSheDWService:
public PageBean queryForPage(int pageSize, int currentPage);
Implement methods in the service implementation class:
public PageBean queryForPage(int pageSize, int page) { final String hql = "from JianSheDWBean"; // Query statement int allRow = this.baseDao.getAllRowCount(hql); // Total records int totalPage = PageBean.countTotalPage(pageSize, allRow); // Total pages final int offset = PageBean.countOffset(pageSize, page); // The current page starts recording final int length = pageSize; // Number of records per page final int currentPage = PageBean.countCurrentPage(page); List<JianSheDWBean> list = this.baseDao.queryForPage(hql, offset, length); // Record of "one page"// Save the paging information into the Bean PageBean pageBean = new PageBean(); pageBean.setPageSize(pageSize); pageBean.setCurrentPage(currentPage); pageBean.setAllRow(allRow); pageBean.setTotalPage(totalPage); pageBean.setList(list); pageBean.init(); return pageBean; } Create a pagination model in view layer action
package com.fishing.action.lcq; import com.fishing.common.bean.JianSheDWBean; import com.fishing.common.bean.PageBean; import com.fishing.service.lcq.JianSheDWService; import com.opensymphony.xwork2.ActionSupport; @SuppressWarnings("serial") public class GetInfoJSDWListAction extends ActionSupport { private int page; // What page private PageBean pageBean; // bean containing distribution information private JianSheDWBean jianSheDWBean; // private PageBean page; private JianSheDWService jianSheDWService; public int getPage() { return page; } public void setPage(int page) { this.page = page; } public PageBean getPageBean() { return pageBean; } public void setPageBean(PageBean pageBean) { this.pageBean = pageBean; } public JianSheDWBean getJianSheDWBean() { return jianSheDWBean; } public void setJianSheDWBean(JianSheDWBean jianSheDWBean) { this.jianSheDWBean = jianSheDWBean; } public JianSheDWService getJianSheDWService() { return jianSheDWService; } public void setJianSheDWService(JianSheDWService jianSheDWService) { this.jianSheDWService = jianSheDWService; } @Override public String execute() throws Exception { //The pageBean of the pagination, the parameter pageSize indicates the number of records displayed per page, the page is the current page this.pageBean = jianSheDWService.queryForPage(10, page); return SUCCESS; } } Write a page in jsp
<tr> <td> </td> <td> <s:if test="%{pageBean.currentPage == 1}"> Home Previous Page</s:if> <s:else> <a href="jiangguan/getJSDWInfos.action?page=1">Home</a> <a href="jiangguan/getJSDWInfos.action?page=<s:property value="%{pageBean.currentPage-1}"/>" /> Previous Page</a> </s:else> </td> <td> <s:if test="%{pageBean.currentPage != pageBean.totalPage}"> <a href="jiangguan/getJSDWInfos.action?page=<s:property value="%{pageBean.currentPage+1}"/>">Next page</a> <a href="jiangguan/getJSDWInfos.action?page=<s:property value="pageBean.totalPage"/>">Last page</a> </s:if> <s:else> Next pageLast page</s:else> </td> <td> <div align="center"> Page<s:property value="pageBean.currentPage" /> / <s:property value="pageBean.totalPage" /> <s:property value="pageBean.allRow" /> records</div> <div align="center"></div> </td> </td> </tr> The above is just an implementation of the code, and does not explain the configuration file, and the reader configures it according to the situation.
I hope this article will be helpful to you. This is all for you to introduce the pagination content of SSH2 implementation of database and interface. I hope everyone will continue to follow our website! If you want to learn Java, you can continue to follow this website.