@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>
发表评论
-
Spring的JdbcTemplate对数据库的增删改操作(备忘)
2013-08-06 23:19 836import org.springframework.jd ... -
CXF 配置备忘
2012-09-11 11:32 822服务端配置: web.xml <web-app&g ... -
java获取明天的日期
2012-08-30 16:14 19391public static void main(Strin ... -
java 字符串日期转换成人性化格式
2012-08-06 11:40 951/** * 转换日期格式 * * @param d ... -
JSTL 标签输出java List<List>
2012-08-03 12:05 953<c:forEach var="zcdCh ... -
[转]java代码实现15位身份证号码升级到18位
2012-08-02 12:38 1207原文地址:http://blog.csdn.net/mhmyq ... -
[转]java实现的18位身份证格式验证算法
2012-08-02 12:38 825原文地址:http://fanqiang.chinaunix. ... -
[转]java实现—身份证号码验证
2012-08-02 12:38 813原文地址:http://akunamotata.iteye.c ... -
[转]java验证身份证号码及编码规则和提取相应信息
2012-08-02 12:37 1203原文地址:http://dreams75.iteye.com/ ... -
Java图片/文件下载+Hibernate+Oracle数据库Blob字段类型
2012-08-02 12:37 1222@RequestMapping(value = " ... -
ExtJs+Spring+Hibernate上传文件/图片到Oracle数据库Blob字段类型
2012-08-01 12:19 3162ExtJs: var fatj_form = new Ex ... -
Java操作数据库工具类
2012-08-01 12:19 1488import java.sql.Connection; ... -
Java读取properties文件工具类
2012-08-01 12:19 1431//DB.properties文件 className= ... -
J2EE项目代码编写规范[转]
2012-07-25 17:01 690代码编写规范目的:能够在编码过程中实现规范化,为以后的程序开发 ... -
常用数据库的JDBC连接代码
2012-07-25 17:01 825数据库 连接代码 M ... -
我的Jdon安装第三步出错解决
2012-07-25 17:01 796因为jive论坛是个很有名开放源代码的论坛,从2.1版本以后开 ...
相关推荐
Java文件工具类
page.java java分页工具类
java工具类java工具类java工具类java工具类java工具类java工具类java工具类java工具类java工具类
[工具类] CookieCounter .java.txt [工具类] 验证码img .jsp.txt [工具类] Java中计算任意两个日期之间的工作天数 .java.txt [工具类] java抓取网页 .java.txt [工具类] MD5 .java.txt [工具类] MD5强化版 .java.txt...
java常用工具类,json,date,http,kafka,md5,线程池
Class类工具 \Cookie工具类 \excel读取 工具类\Java如何生成验证码图片和点击刷新验证码\java获取当前月第一天和最后一天,上个月第一天和最后一天\java实现ftp文件的上传与下载\Json工具类 - JsonUtils.java\JS...
使用java工具类可有效的提高开发效率! 没有CSDN积分的朋友到这里源头下载:http://www.javacs.cn/bbs/thread-382-1-1.html 感谢支持 [工具类] CookieCounter .java.txt [工具类] 验证码img .jsp.txt [工具类] Java中...
java常用的工具类整理28个java常用的工具类整理28个java常用的工具类整理28个
Java实现的Https请求工具类。 https的java工具类 Https
java常用工具类封装,线程池、elasticsearch高级客户端集成、word,pdf,excel等文档解析
Java数组工具类
java分页工具类 java分页工具类 java分页工具类 java分页工具类 java分页工具类 java分页工具类
NULL 博文链接:https://wenxiang-tune.iteye.com/blog/846379
该工具类下载后拷贝到你要用的包,只需该包名即可
java utils 工具类
1.[工具类] 读取、打印输出、保存xml .java 2.[工具类] Java中计算任意两个日期之间的工作天数 .java 3.[工具类] MD5 .java 4.[工具类] 时间工具TimeUtil.java 5.[工具类] 通信服务端simpleServer 6.[工具类] 使用...
java转码 工具类,
Java常用工具类大全,工作5年精心整理.zip
java各种工具类:包括日期工具类、字符串工具类、文件工具类、xml读写工具类、json工具类、http访问工具类、压缩包工具类等等。非常齐全
1、java工具类RabbitmqUtil是本人封装好的直接使用 2、常用的方法都在里面 3、改一下初始化配置参数就行