六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 25|回复: 0

Java 翻页工具类

[复制链接]

升级  50.67%

108

主题

108

主题

108

主题

举人

Rank: 3Rank: 3

积分
352
 楼主| 发表于 2013-2-3 13:39:58 | 显示全部楼层 |阅读模式
@RequestMapping(value = "list.action")public String list(HttpServletRequest request, HttpServletResponse response) {int _pageSize = 20;//每页显示数量int _totalRecord = cmsArticleSer.countText();//总记录数int _currentPage = 1;//当前页码PageUtil page = new PageUtil();// 当前页码if (request.getParameter("pageNo") != null && !request.getParameter("pageNo").equals("")) {_currentPage = Integer.parseInt(request.getParameter("pageNo"));}page.setPageSize(_pageSize);// 每页显示的记录数量page.setTotalRecord(_totalRecord);// 总记录数page.setCurrentPage(_currentPage);// 当前页码cu.setStart(page.getStart());//开始记录数cu.setLimit(page.getPageSize());//一次提取多少条记录List<Test> list = getTestList(cu);request.setAttribute("list", list);this.setPage(page, request);return "cms/list";}public void setPage(PageUtil page, HttpServletRequest request) {// 每页显示的记录数量request.setAttribute("pageSize", page.getPageSize());// 总记录数request.setAttribute("totalRecord", page.getTotalRecord());// 当前页码request.setAttribute("currentPage", page.getCurrentPage());// 总页数request.setAttribute("totalPage", page.getTotalPage());// 当前页开始记录request.setAttribute("currentStartRecord", page.getCurrentStartRecord());// 当前页结束记录request.setAttribute("currentEndRecord", page.getCurrentEndRecord());// 向上翻页页码request.setAttribute("upPage", page.getUpPage());// 向下翻页页码request.setAttribute("downPage", page.getDownPage());}//数据库操作@SuppressWarnings("unchecked")public List<Test> getTestList(TestUtil cu) {List<Test> list = new ArrayList<CmsArticle>();StringBuffer sb = new StringBuffer();try {sb.append("from Test ");Query query = this.getSession().createQuery(sb.toString());query.setFirstResult(cu.getStart());query.setMaxResults(cu.getLimit());list = query.list();} catch (Exception e) {System.out.println(e);}return list;}/** * 翻页类 1.首先设置 每页显示的记录数量 2.再次设置 总记录数 3.最后设置 当前页码 *  * @author Administrator */public class PageUtil {private int pageSize;// 每页显示的记录数量private int totalRecord;// 总记录数private int currentPage;// 当前页码// ***********************************************private int totalPage;// 总页数private int currentStartRecord;// 当前页开始记录private int currentEndRecord; // 当前页结束记录private int start;// 开始记录private int end;// 结束记录private int upPage;// 向上翻页页码private int downPage;// 向下翻页页码public int getPageSize() {return pageSize;}/** * 设置每页显示的记录数量 *  * @param pageSize */public void setPageSize(int pageSize) {this.pageSize = pageSize;}public int getTotalRecord() {return totalRecord;}/** * 设置总记录数 */public void setTotalRecord(int totalRecord) {this.totalRecord = totalRecord;// 设置总页数int _totalPage = 1;if (getTotalRecord() % getPageSize() == 0) {// 如果总记录数除以每页显示条数可以整除,商就是总页码_totalPage = this.getTotalRecord() / this.getPageSize();} else {// 如果总记录数除以每页显示条数不能整除,商加1才是总页码_totalPage = this.getTotalRecord() / this.getPageSize() + 1;}if (_totalPage < 1) {_totalPage = 1;}this.setTotalPage(_totalPage);}public int getCurrentPage() {return currentPage;}/** * 设置页码 */public void setCurrentPage(int currentPage) {if (currentPage > this.getTotalPage()) {this.currentPage = this.getTotalPage();} else {this.currentPage = currentPage;}// 设置当前页开始记录int _currentStartRecord = 0;if (this.getTotalRecord() != 0) {_currentStartRecord = (this.getCurrentPage() - 1)* this.getPageSize() + 1;}if (_currentStartRecord < 0) {_currentStartRecord = 0;}this.setCurrentStartRecord(_currentStartRecord);// 设置当前页结束记录int _currentEndRecord = this.getCurrentPage() * this.getPageSize();if (_currentEndRecord > this.getTotalRecord()) {_currentEndRecord = this.getTotalRecord();}this.setCurrentEndRecord(_currentEndRecord);// 开始记录int _start = (this.getCurrentPage() - 1) * this.getPageSize();if (_start < 0) {_start = 0;}this.setStart(_start);// 结束记录int _end = this.getCurrentPage() * this.getPageSize();if (_end > this.getTotalRecord()) {_end = this.getTotalRecord();}this.setEnd(_end);// 向上翻页页码int _upPage = this.getCurrentPage() - 1;if (_upPage < 1) {_upPage = 1;}this.setUpPage(_upPage);// 向下翻页页码int _downPage = this.getCurrentPage() + 1;if (_downPage > this.getTotalPage()) {_downPage = this.getTotalPage();}this.setDownPage(_downPage);}public int getTotalPage() {return totalPage;}public void setTotalPage(int totalPage) {this.totalPage = totalPage;}public int getCurrentStartRecord() {return currentStartRecord;}public void setCurrentStartRecord(int currentStartRecord) {this.currentStartRecord = currentStartRecord;}public int getCurrentEndRecord() {return currentEndRecord;}public void setCurrentEndRecord(int currentEndRecord) {this.currentEndRecord = currentEndRecord;}public int getStart() {return start;}public void setStart(int start) {this.start = start;}public int getEnd() {return end;}public void setEnd(int end) {this.end = end;}public int getUpPage() {return upPage;}public void setUpPage(int upPage) {this.upPage = upPage;}public int getDownPage() {return downPage;}public void setDownPage(int downPage) {this.downPage = downPage;}}

jsp页面
<table width="93%" height="39" border="0" align="center"cellpadding="0" cellspacing="0" class="title_bg"><tr><td><form id="qbmh_list_form" name="qbmh_list_form" method="post" action="list2.action"><c:if test="${currentPage == '1'}">首页</c:if><c:if test="${currentPage != '1'}"><a href="javascript: gotoPage('1')">首页</a><a href="javascript: gotoPage('${upPage}')">上一页</a> </c:if>第<input name="pageNo" id="pageNo" type="text" class="testpage" value="${currentPage}" />页共${totalRecord}条记录每页${pageSize}条记录共${totalPage}页第${currentStartRecord}条—第${currentEndRecord}条 记录<c:if test="${currentPage != totalPage}"><a href="javascript: gotoPage('${downPage}')">下一页<a href="javascript: gotoPage('${totalPage}')">尾页</a></c:if><c:if test="${currentPage == totalPage}">尾页</c:if></form></td></tr></table>

<script type="text/javascript" language="javascript">//键盘捕捉function document.onkeydown(){//回车if(event.keyCode == '13'){var _pageNo = document.getElementById("pageNo").value;gotoPage(_pageNo);}}//翻页function gotoPage(pageNo){var re = /^[1-9]\d*$/;if (!re.test(pageNo)) {pageNo="1";}document.getElementById("pageNo").value=pageNo;document.getElementById("qbmh_list_form").submit();}</script>
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表