struts2中分页简单实现

来源:互联网 发布:java群ping 编辑:程序博客网 时间:2024/05/19 10:40
<pre name="code" class="java">public class PageBean {// 传递的参数或是配置的参数private int currentPage; // 当前页private int pageSize; // 每页显示多少条记录// 查询数据库private List recordList; // 本页的数据列表private int recordCount; // 总记录数// 计算private int pageCount; // 总页数private int beginPageIndex; // 页码列表的开始索引(包含)private int endPageIndex; // 页码列表的结束索引(包含)/** * 只接受4个必要的属性,会自动的计算出其他3个属性的值 *  * @param currentPage * @param pageSize * @param recordList * @param recordCount */public PageBean(int currentPage, int pageSize, List recordList, int recordCount) {this.currentPage = currentPage;this.pageSize = pageSize;this.recordList = recordList;this.recordCount = recordCount;// 计算 pageCountpageCount = (recordCount + pageSize - 1) / pageSize; // 计算 beginPageIndex 与 endPageIndex// >> 总页码小于等于10页时,全部显示if (pageCount <= 10) {beginPageIndex = 1;endPageIndex = pageCount;}// >> 总页码大于10页时,就只显示当前页附近的共10个页码else {// 默认显示 前4页 + 当前页 + 后5页beginPageIndex = currentPage - 4; // 7 - 4 = 3;endPageIndex = currentPage + 5; // 7 + 5 = 12; --> 3 ~ 12// 如果前面不足4个页码时,则显示前10页if (beginPageIndex < 1) {beginPageIndex = 1;endPageIndex = 10;}// 如果后面不足5个页码时,则显示后10页else if (endPageIndex > pageCount) {endPageIndex = pageCount;beginPageIndex = pageCount - 9;}}}public List getRecordList() {return recordList;}public void setRecordList(List recordList) {this.recordList = recordList;}public int getCurrentPage() {return currentPage;}public void setCurrentPage(int currentPage) {this.currentPage = currentPage;}public int getPageCount() {return pageCount;}public void setPageCount(int pageCount) {this.pageCount = pageCount;}public int getPageSize() {return pageSize;}public void setPageSize(int pageSize) {this.pageSize = pageSize;}public int getRecordCount() {return recordCount;}public void setRecordCount(int recordCount) {this.recordCount = recordCount;}public int getBeginPageIndex() {return beginPageIndex;}public void setBeginPageIndex(int beginPageIndex) {this.beginPageIndex = beginPageIndex;}public int getEndPageIndex() {return endPageIndex;}public void setEndPageIndex(int endPageIndex) {this.endPageIndex = endPageIndex;}}


<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@ taglib prefix="s" uri="/struts-tags"%><div id=PageSelectorBar><div id=PageSelectorMemo>页次:${currentPage}/${pageCount}页  每页显示:${pageSize }条  总记录数:${recordCount }条</div><div id=PageSelectorSelectorArea><a href="javascript: gotoPage(1)" title="首页" style="cursor: hand;"><img src="${pageContext.request.contextPath}/style/blue/images/pageSelector/firstPage.png"/></a><s:iterator begin="%{beginPageIndex}" end="%{endPageIndex}" var="num"><s:if test="currentPage == #num"><%-- 当前页 --%><span class="PageSelectorNum PageSelectorSelected">${num}</span></s:if><s:else><%-- 非当前页 --%><span class="PageSelectorNum" style="cursor: hand;" onClick="gotoPage(${num});">${num}</span></s:else></s:iterator><a href="javascript: gotoPage(${pageCount})" title="尾页" style="cursor: hand;"><img src="${pageContext.request.contextPath}/style/blue/images/pageSelector/lastPage.png"/></a>转到:<select id="pn" onchange="gotoPage(this.value)"><s:iterator begin="1" end="%{pageCount}" var="num"><option value="${num}">${num}</option></s:iterator></select><script type="text/javascript">// 回显页码$("#pn").val(${currentPage});</script></div></div>

0 0
原创粉丝点击