Java封装分页组件,在JSP中进行分页
2013-05-09 23:33
453 查看
分页离不开两个必须的量①当前页②每页显示几条记录,有了这两个量,就可以构建分页所需的各种量了,如总页数,页面页码的开始索引,结束索引等。
一、分页组件
其中,页码索引类如下:
QueryResult是查询结果类,是对到DB中检索出来的数据的封装,可以直接放进PageView中
二、下面可以把分页单独抽出为一个JSP,需要分页时,直接include即可
三、现在就可以应用封装好的分页组件了
结果页面如下:
一、分页组件
package edu.njcit.action; import java.util.List; /** * 分页组件 * @author Administrator * @param <T> */ public class PageView<T> { /** 分页数据 **/ private List<T> records; /** 页码开始索引和结束索引 **/ private PageIndex pageindex; /** 总页数 **/ private long totalpage = 1; /** 每页显示记录数,默认12 **/ private int recordPerPage=12; /** 当前页 **/ private int currentpage = 1; /** 总记录数 **/ private long totalrecord; /** 页码数量,默认10 **/ private int pagecode = 10; /** 获取记录的开始索引 **/ public int getStartRecordIndex() { return (this.currentpage-1)*this.recordPerPage; } public int getPagecode() { return pagecode; } public void setPagecode(int pagecode) { this.pagecode = pagecode; } /** * 构造函数 * @param recordPerPage 每页显示的记录数 * @param currentpage 当前页 */ public PageView(int recordPerPage, int currentpage) { this.recordPerPage = recordPerPage; this.currentpage = currentpage; } /** * 构造函数 * @param recordPerPage 每页显示的记录数 * @param currentpage 当前页 * @param pagecode 页码数量 */ public PageView(int recordPerPage, int currentpage,int pagecode) { this.recordPerPage = recordPerPage; this.currentpage = currentpage; this.pagecode=pagecode; } public void setQueryResult(QueryResult<T> qr){ setTotalrecord(qr.getTotalrecord()); setRecords(qr.getResultlist()); } public long getTotalrecord() { return totalrecord; } public void setTotalrecord(long totalrecord) { this.totalrecord = totalrecord; //总记录数变化时,要重新计算总页码和页码开始,结束索引 setTotalpage(this.totalrecord%this.recordPerPage==0? this.totalrecord/this.recordPerPage : this.totalrecord/this.recordPerPage+1); } public List<T> getRecords() { return records; } public void setRecords(List<T> records) { this.records = records; } public PageIndex getPageindex() { return pageindex; } public long getTotalpage() { return totalpage; } public void setTotalpage(long totalpage) { this.totalpage = totalpage; this.pageindex = PageIndex.getPageIndex(pagecode, currentpage, totalpage); } public int getRecordPerPage() { return recordPerPage; } public int getCurrentpage() { return currentpage; } }
其中,页码索引类如下:
package edu.njcit.action; /** * 页面页码类 * @author Administrator */ public class PageIndex { private long startindex;//开始页码 private long endindex;//结束页码 public PageIndex(long startindex, long endindex) { this.startindex = startindex; this.endindex = endindex; } public long getStartindex() { return startindex; } public void setStartindex(long startindex) { this.startindex = startindex; } public long getEndindex() { return endindex; } public void setEndindex(long endindex) { this.endindex = endindex; } /** * 根据页码数量,当前页,总页数构建页码索引 * @param pagecode 页码数量 * @param currentPage 当前页 * @param totalpage 总页数 * @return */ public static PageIndex getPageIndex(long pagecode, int currentPage, long totalpage){ long startpage = currentPage-(pagecode%2==0? pagecode/2-1 : pagecode/2); long endpage = currentPage+pagecode/2; if(startpage<1){ startpage = 1; if(totalpage>=pagecode) endpage = pagecode; else endpage = totalpage; } if(endpage>totalpage){ endpage = totalpage; if((endpage-pagecode)>0) startpage = endpage-pagecode+1; else startpage = 1; } return new PageIndex(startpage, endpage); } }
QueryResult是查询结果类,是对到DB中检索出来的数据的封装,可以直接放进PageView中
package edu.njcit.action; import java.util.List; /** * 查询结果 * @author Administrator * @param <T> */ public class QueryResult<T> { private List<T> resultlist; /** * 页面看到的总记录数 * 默认情况下=resultlist.size(),也可自行指定 */ private long totalrecord;// public List<T> getResultlist() { return resultlist; } public void setResultlist(List<T> resultlist) { this.resultlist = resultlist; } public long getTotalrecord() { return totalrecord; } public void setTotalrecord(long totalrecord) { this.totalrecord = totalrecord; } public QueryResult(List<T> resultlist, long totalrecord) { super(); this.resultlist = resultlist; this.totalrecord = totalrecord; } }
二、下面可以把分页单独抽出为一个JSP,需要分页时,直接include即可
<%@ page language="java" pageEncoding="UTF-8"%> <font color="black"> 当前页:第${pageView.currentpage}页 | 总记录数:${pageView.totalrecord}条 | 每页显示:${pageView.recordPerPage}条 | 总页数:${pageView.totalpage}页 </font> <br/> <c:forEach begin="${pageView.pageindex.startindex}" end="${pageView.pageindex.endindex}" var="wp"> <c:if test="${pageView.currentpage==wp}"><b><font color="gray">第${wp}页</font></b></c:if> <c:if test="${pageView.currentpage!=wp}"><a href="javascript:topage('${wp}')" class="a03">第${wp}页</a></c:if> </c:forEach>这里需要一段JS代码,用于当点击第几页时更改currentPage和提交表单:
//到指定的分页页面 function topage(page){ var form = document.forms[0];//包含currentPage hidden框的form form.currentPage.value=page; form.submit(); }
三、现在就可以应用封装好的分页组件了
package edu.njcit.action.paging; import java.util.ArrayList; import java.util.Date; import java.util.List; import com.opensymphony.xwork2.ActionSupport; import edu.njcit.action.ActionSupportBase; import edu.njcit.action.PageView; import edu.njcit.action.QueryResult; import edu.njcit.entity.Student; public class PagingAction extends ActionSupportBase { private static final long serialVersionUID = 1L; @Override public String execute() throws Exception { List<Student> list=new ArrayList<Student>(); //模拟数据 for (int i = 1; i <= 300; i++) { Student student=new Student(); student.setId(String.valueOf(i)); student.setBirth(new Date()); student.setAge(i); student.setName((i)+""+i); list.add(student); } PageView<Student> pageView=new PageView<Student>(12,getCurrentPage()); int start=pageView.getStartRecordIndex(); int lenght=pageView.getRecordPerPage(); QueryResult<Student> qr=new QueryResult<Student>(list.subList(start, start+lenght),list.size()); pageView.setQueryResult(qr); request.setAttribute("pageView", pageView); return ActionSupport.SUCCESS; } }
结果页面如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ include file="/taglib.jsp" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <link rel="stylesheet" type="text/css" href="/css/style.css"> <script language="javascript" src="/js/topage.js"></script> </head> <body> 学生如下:<br/> <s:form action="paging" method="post"> <s:hidden name="currentPage"></s:hidden> <table style="border:solid 1px green;"> <tr><td>学号</td><td>姓名</td><td>年龄</td><td>生日</td></tr> <c:forEach items="${pageView.records}" var="entry"> <tr> <td>${entry.id}</td> <td>${entry.name}</td> <td>${entry.age}</td> <td><fmt:formatDate value="${entry.birth}" type="both" pattern="yyyy-MM-dd" /></td> </tr> </c:forEach> </table> </s:form> <!-- 将分页JSP包含进来 --> <%@ include file="/fenye.jsp" %> </body> </html>最终效果如下:
相关文章推荐
- 利用组件对翻页逻辑进行封装--JSP及JSP页面的翻页
- jsp+java bean+mysql数据库进行分页显示
- java jsp标签分页,jquery 插件分页封装
- 对分页控件进行分页的封装
- 用jsp进行数据分页显示的一个实现
- jsp+servlet+javaBean的后台分页
- Java之使用getXxx()或setXxx对类进行封装
- Java之使用getXxx()或setXxx对类进行封装text1
- Java Web中常用的分页组件(Java端实现)
- 使用jspSmartUpload组件进行文件上传、下载
- java使用mongo数据库,怎么根据两列进行分组统计并分页
- Ajax进行删除更新分页显示页面(JSP+Struts2+Poolman)
- jsp页面与后台进行交流传值(java)
- java(安全方便的从控制台读入数据)[对Scanner类进行封装,用正则表达式判断]
- 采用jspSmartUpload组件进行文件的上传
- page-taglib标签结合SSH框架进行java分页
- 基于jQuery封装的分页组件
- php+mysql的分页进行封装调用
- 一个JSP分页组件