java分页 Struts+Ibatis
来源:互联网 发布:head first python知乎 编辑:程序博客网 时间:2024/06/05 03:13
首先写一个分页类:
- package com.zc.book.util;
- import java.util.List;
- public class PageUtil {
- private int pageSize=20;//每页显示的条数 默认20
- private int recordCount;//记录总数
- private int currentPage;//当前页
- private int pageIndex;//每页的第一条记录编号
- private List pageList;//每页的记录集
- private int totalPage;//总页数
- 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 getCurrentPage() {
- return currentPage;
- }
- public void setCurrentPage(int currentPage) {
- this.currentPage = currentPage;
- }
- public int getPageIndex() {
- return pageIndex;
- }
- public void setPageIndex(int pageIndex) {
- this.pageIndex = pageIndex;
- }
- public List getPageList() {
- return pageList;
- }
- public void setPageList(List pageList) {
- this.pageList = pageList;
- }
- public int getTotalPage() {
- return totalPage;
- }
- public void setTotalPage(int totalPage) {
- this.totalPage = totalPage;
- }
- //初始化 currentPage 当前页,pageSize 每页大小,recordCount 总记录数,pageIndex 每页第一条记录序号
- public void init(int currentPage,int pageSize,int recordCount){
- this.currentPage=currentPage>0?currentPage:1;//设置当前页
- this.pageSize=pageSize;//设置每页大小
- this.recordCount=recordCount;//设置总记录数
- this.pageIndex=(currentPage-1)*pageSize;//设置每页第一条记录
- this.totalPage=recordCount/pageSize+1;
- if(currentPage>totalPage){
- currentPage=totalPage;
- }
- }
- }
在Action中
- int currentPage = 1;
- try {
- currentPage = Integer.parseInt(request.getParameter("currentPage"));
- } catch(Exception ex) {
- }
- PageUtil pageUtil=new PageUtil();
- pageUtil=bookService.pageBookInfo(currentPage);
- request.setAttribute("pageUtil", pageUtil);
- return mapping.findForward("bookInfo");
service中的pageBookInfo方法:
- public PageUtil pageBookInfo(int currentPage) {
- // TODO Auto-generated method stub
- List list=bookDao.bookInfo();
- PageUtil pageUtil=new PageUtil();
- //设置当前页,每页记录数,总记录数
- pageUtil.init(currentPage, 25, list.size());
- //通过pageUtil的"pageIndex--每页的第一条记录编号"和"pageSize--每页显示的条数"
- //这两个参数去数据库中查询出其中一页的记录,放入List里
- list=bookDao.pageBook(pageUtil);
- //将list中一页的记录放入pageUtil
- pageUtil.setPageList(list);
- return pageUtil;
- }
sqlMap中的语句:
- <select id="listAllBook" resultClass="Book">
- select * from bookinfo
- </select>
- <select id="pageBook" resultClass="Book" parameterClass="PageUtil">
- select * from bookinfo limit #pageIndex#,#pageSize#
- </select>
在jsp中添加:
- <table width="100%" border="0" cellspacing="0" cellpadding="0">
- <tr>
- <td width="25%" height="29" nowrap="nowrap"><span class="STYLE1">共${pageUtil.recordCount }条纪录,当前第${pageUtil.currentPage }/${pageUtil.totalPage }页,每页${pageUtil.pageSize }条纪录</span></td>
- <td width="75%" valign="top" class="STYLE1"><div align="right">
- <table width="352" height="20" border="0" cellpadding="0" cellspacing="0">
- <tr>
- <c:if test="${pageUtil.currentPage == 1}">
- <td width="62" height="22" valign="middle"><div align="right"><img src="tab/images/first.gif" mce_src="tab/images/first.gif" width="37" height="15" /></div></td>
- <td width="50" height="22" valign="middle"><div align="right"><img src="tab/images/back.gif" mce_src="tab/images/back.gif" width="43" height="15" /></div></td>
- </c:if>
- <c:if test="${pageUtil.currentPage != 1}">
- <td width="62" height="22" valign="middle" align="right" ><a href="<%=basePath%>book.do?method=page¤tPage=1" target='I1'><img src="tab/images/first.gif" mce_src="tab/images/first.gif" width="37" height="15" border="0"/></a></td>
- <td width="50" height="22" valign="middle" align="right"><a href="<%=basePath%>book.do?method=page¤tPage=${pageUtil.currentPage-1 }" target='I1'><img src="tab/images/back.gif" mce_src="tab/images/back.gif" width="43" height="15" border="0"></a></td>
- </c:if>
- <c:if test="${pageUtil.currentPage == pageUtil.totalPage}">
- <td width="54" height="22" valign="middle" align="right"><img src="tab/images/next.gif" mce_src="tab/images/next.gif" width="43" height="15" /></td>
- <td width="49" height="22" valign="middle" align="right"><img src="tab/images/last.gif" mce_src="tab/images/last.gif" width="37" height="15" /></td>
- </c:if>
- <c:if test="${pageUtil.currentPage != pageUtil.totalPage}">
- <td width="54" height="22" valign="middle" align="right"><a href="<%=basePath%>book.do?method=page¤tPage=${pageUtil.currentPage+1 }" target='I1'><img src="tab/images/next.gif" mce_src="tab/images/next.gif" width="43" height="15" border="0"/></a></td>
- <td width="49" height="22" valign="middle" align="right"><a href="<%=basePath%>book.do?method=page¤tPage=${pageUtil.totalPage }" target='I1'><img src="tab/images/last.gif" mce_src="tab/images/last.gif" width="37" height="15" border="0"/></a></td>
- </c:if>
- <td width="59" height="22" valign="middle"><div align="right"><span class="STYLE1">转到第</span></div></td>
- <td width="25" height="22" valign="middle"><span class="STYLE7">
- <!-- <input name="textfield" type="text" class="STYLE1" style="height:15px; width:30px;" size="5" /> -->
- <select id="selectPage" name="selectPage">
- <c:forEach var="i" begin="1" end="${pageUtil.totalPage }" step="1">
- <option value="${i}"><c:out value="${i}" /></option>
- </c:forEach>
- </select>
- </span></td>
- <td width="23" height="22" valign="middle"><span class="STYLE1">页</span></td>
- <td width="30" height="22" valign="middle"><a href="javascript:goPage()" mce_href="javascript:goPage()"><img src="tab/images/go.gif" mce_src="tab/images/go.gif" width="37" height="15" border="0"/></a></td>
- </tr>
- </table>
- </div></td>
- <td width="14"><img src="tab/images/tab_22.gif" mce_src="tab/images/tab_22.gif" width="14" height="29" /></td>
- </tr>
- </table>
- <mce:script type="text/javascript"><!--
- //页数select随页数自动变化
- function selectPage(currentPage){
- var i,num,op
- num=document.getElementById("selectPage").length;
- for(i=0;i<num;i++){
- op=document.getElementById("selectPage").options[i];
- if(op.value==currentPage) op.selected=true;
- }
- }
- //根据所选页数进行跳转
- function goPage(){
- var currentpage;
- currentpage=document.getElementById("selectPage").value;
- window.location="<%=basePath%>book.do?method=page¤tPage="+currentpage;
- }
- // --></mce:script>
首先写一个分页类:
- package com.zc.book.util;
- import java.util.List;
- public class PageUtil {
- private int pageSize=20;//每页显示的条数 默认20
- private int recordCount;//记录总数
- private int currentPage;//当前页
- private int pageIndex;//每页的第一条记录编号
- private List pageList;//每页的记录集
- private int totalPage;//总页数
- 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 getCurrentPage() {
- return currentPage;
- }
- public void setCurrentPage(int currentPage) {
- this.currentPage = currentPage;
- }
- public int getPageIndex() {
- return pageIndex;
- }
- public void setPageIndex(int pageIndex) {
- this.pageIndex = pageIndex;
- }
- public List getPageList() {
- return pageList;
- }
- public void setPageList(List pageList) {
- this.pageList = pageList;
- }
- public int getTotalPage() {
- return totalPage;
- }
- public void setTotalPage(int totalPage) {
- this.totalPage = totalPage;
- }
- //初始化 currentPage 当前页,pageSize 每页大小,recordCount 总记录数,pageIndex 每页第一条记录序号
- public void init(int currentPage,int pageSize,int recordCount){
- this.currentPage=currentPage>0?currentPage:1;//设置当前页
- this.pageSize=pageSize;//设置每页大小
- this.recordCount=recordCount;//设置总记录数
- this.pageIndex=(currentPage-1)*pageSize;//设置每页第一条记录
- this.totalPage=recordCount/pageSize+1;
- if(currentPage>totalPage){
- currentPage=totalPage;
- }
- }
- }
在Action中
- int currentPage = 1;
- try {
- currentPage = Integer.parseInt(request.getParameter("currentPage"));
- } catch(Exception ex) {
- }
- PageUtil pageUtil=new PageUtil();
- pageUtil=bookService.pageBookInfo(currentPage);
- request.setAttribute("pageUtil", pageUtil);
- return mapping.findForward("bookInfo");
service中的pageBookInfo方法:
- public PageUtil pageBookInfo(int currentPage) {
- // TODO Auto-generated method stub
- List list=bookDao.bookInfo();
- PageUtil pageUtil=new PageUtil();
- //设置当前页,每页记录数,总记录数
- pageUtil.init(currentPage, 25, list.size());
- //通过pageUtil的"pageIndex--每页的第一条记录编号"和"pageSize--每页显示的条数"
- //这两个参数去数据库中查询出其中一页的记录,放入List里
- list=bookDao.pageBook(pageUtil);
- //将list中一页的记录放入pageUtil
- pageUtil.setPageList(list);
- return pageUtil;
- }
sqlMap中的语句:
- <select id="listAllBook" resultClass="Book">
- select * from bookinfo
- </select>
- <select id="pageBook" resultClass="Book" parameterClass="PageUtil">
- select * from bookinfo limit #pageIndex#,#pageSize#
- </select>
在jsp中添加:
- <table width="100%" border="0" cellspacing="0" cellpadding="0">
- <tr>
- <td width="25%" height="29" nowrap="nowrap"><span class="STYLE1">共${pageUtil.recordCount }条纪录,当前第${pageUtil.currentPage }/${pageUtil.totalPage }页,每页${pageUtil.pageSize }条纪录</span></td>
- <td width="75%" valign="top" class="STYLE1"><div align="right">
- <table width="352" height="20" border="0" cellpadding="0" cellspacing="0">
- <tr>
- <c:if test="${pageUtil.currentPage == 1}">
- <td width="62" height="22" valign="middle"><div align="right"><img src="tab/images/first.gif" mce_src="tab/images/first.gif" width="37" height="15" /></div></td>
- <td width="50" height="22" valign="middle"><div align="right"><img src="tab/images/back.gif" mce_src="tab/images/back.gif" width="43" height="15" /></div></td>
- </c:if>
- <c:if test="${pageUtil.currentPage != 1}">
- <td width="62" height="22" valign="middle" align="right" ><a href="<%=basePath%>book.do?method=page¤tPage=1" target='I1'><img src="tab/images/first.gif" mce_src="tab/images/first.gif" width="37" height="15" border="0"/></a></td>
- <td width="50" height="22" valign="middle" align="right"><a href="<%=basePath%>book.do?method=page¤tPage=${pageUtil.currentPage-1 }" target='I1'><img src="tab/images/back.gif" mce_src="tab/images/back.gif" width="43" height="15" border="0"></a></td>
- </c:if>
- <c:if test="${pageUtil.currentPage == pageUtil.totalPage}">
- <td width="54" height="22" valign="middle" align="right"><img src="tab/images/next.gif" mce_src="tab/images/next.gif" width="43" height="15" /></td>
- <td width="49" height="22" valign="middle" align="right"><img src="tab/images/last.gif" mce_src="tab/images/last.gif" width="37" height="15" /></td>
- </c:if>
- <c:if test="${pageUtil.currentPage != pageUtil.totalPage}">
- <td width="54" height="22" valign="middle" align="right"><a href="<%=basePath%>book.do?method=page¤tPage=${pageUtil.currentPage+1 }" target='I1'><img src="tab/images/next.gif" mce_src="tab/images/next.gif" width="43" height="15" border="0"/></a></td>
- <td width="49" height="22" valign="middle" align="right"><a href="<%=basePath%>book.do?method=page¤tPage=${pageUtil.totalPage }" target='I1'><img src="tab/images/last.gif" mce_src="tab/images/last.gif" width="37" height="15" border="0"/></a></td>
- </c:if>
- <td width="59" height="22" valign="middle"><div align="right"><span class="STYLE1">转到第</span></div></td>
- <td width="25" height="22" valign="middle"><span class="STYLE7">
- <!-- <input name="textfield" type="text" class="STYLE1" style="height:15px; width:30px;" size="5" /> -->
- <select id="selectPage" name="selectPage">
- <c:forEach var="i" begin="1" end="${pageUtil.totalPage }" step="1">
- <option value="${i}"><c:out value="${i}" /></option>
- </c:forEach>
- </select>
- </span></td>
- <td width="23" height="22" valign="middle"><span class="STYLE1">页</span></td>
- <td width="30" height="22" valign="middle"><a href="javascript:goPage()" mce_href="javascript:goPage()"><img src="tab/images/go.gif" mce_src="tab/images/go.gif" width="37" height="15" border="0"/></a></td>
- </tr>
- </table>
- </div></td>
- <td width="14"><img src="tab/images/tab_22.gif" mce_src="tab/images/tab_22.gif" width="14" height="29" /></td>
- </tr>
- </table>
- <mce:script type="text/javascript"><!--
- //页数select随页数自动变化
- function selectPage(currentPage){
- var i,num,op
- num=document.getElementById("selectPage").length;
- for(i=0;i<num;i++){
- op=document.getElementById("selectPage").options[i];
- if(op.value==currentPage) op.selected=true;
- }
- }
- //根据所选页数进行跳转
- function goPage(){
- var currentpage;
- currentpage=document.getElementById("selectPage").value;
- window.location="<%=basePath%>book.do?method=page¤tPage="+currentpage;
- }
- // --></mce:script>
0 0
- java分页 Struts+Ibatis
- java分页 Struts+Ibatis
- Flex+Java+Struts分页
- iBatis开发详解(5)-----------分页(Struts、freeMark)
- iBATIS分页
- ibatis 分页
- ibatis 分页
- ibatis分页 .
- ibatis 分页
- iBatis分页
- iBatis分页
- Java in Action - Struts分页显示
- Java SSH/SSI框架科普(Struts+Spring+Hibernate/Ibatis)
- struts分页
- Struts分页
- struts分页
- struts分页
- Struts分页
- 超详细的Python实现新浪微博模拟登陆(小白都能懂)
- 百度地图API的简单应用
- 封装表单验证解决一站式项目需求
- C语言名企面试题
- 基数排序
- java分页 Struts+Ibatis
- 如何在github上搭建自己的个人主页
- [PAT-乙级]1007.素数对猜想
- Python学习之文件(二)
- Qt4开发环境搭建(Qt4.8.7+mingw4.8.2+Qt Creator4.2.0)
- 【网络编程】——UDP和TCP通信方式
- jdbc 连接数据库类
- LeetCode#89. Gray Code(格雷码)
- JAVA的访问修饰符、内部类、初始化块