一个通用的Java分页基类代码详解
程序员文章站
2023-12-14 22:44:04
分页的基类
import java.util.list;
/**
* 分页显示的标准类,基本操作,是先给予-当前页数一共的数据条数-每页显示的条数,
*...
分页的基类
import java.util.list; /** * 分页显示的标准类,基本操作,是先给予-当前页数一共的数据条数-每页显示的条数, * 然后在初始化该类,得到总共页数,和开始序号和结束序号, * 然后数据库分页用到开始序号和结束序号,得到数据集合后赋值给该类的list属性, * * 然后把该类发送到jsp页面,进行访问 * @author admin * * @param <t> */ public class pagebean<t> { private int pageindex; //当前页数 private int pagesize; //一共的页数 private int count; //数据条数 private int pagecount; //每页的数据条数 private int start; //起始数据位置 private int end; //结束 private list<t> list=null; public void init(){ /*根count 和pagecount计算页数pagesize */ int pagesize_x=(int)count/pagecount; if(count>=pagecount){ this.pagesize=count%pagecount==0?pagesize_x:pagesize_x+1; } else{ this.pagesize=1; } //判断页数和当前页数 if(pageindex>pagesize){ pageindex=pagesize; } if(pageindex<1){ pageindex=1; } //根据当前页计算起始和结束条目 this.start=(pageindex-1)*pagecount+1; this.end=pageindex*pagecount; } public pagebean(int pageindex, int count, int pagecount) { super(); this.pageindex = pageindex; this.count = count; this.pagecount = pagecount; } public pagebean(int pageindex, int count, int pagecount, list<t> list) { super(); this.pageindex = pageindex; this.count = count; this.pagecount = pagecount; this.list = list; } public pagebean() { super(); // todo auto-generated constructor stub } @override public string tostring() { return "pagebean [count=" + count + ", end=" + end + ", list=" + list + ", pagecount=" + pagecount + ", pageindex=" + pageindex + ", pagesize=" + pagesize + ", start=" + start + "]"; } public int getpageindex() { return pageindex; } public void setpageindex(int pageindex) { this.pageindex = pageindex; } public int getpagesize() { return pagesize; } public void setpagesize(int pagesize) { this.pagesize = pagesize; } public int getcount() { return count; } public void setcount(int count) { this.count = count; } public int getpagecount() { return pagecount; } public void setpagecount(int pagecount) { this.pagecount = pagecount; } 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 list<t> getlist() { return list; } public void setlist(list<t> list) { this.list = list; } }
servlet调用
import java.io.ioexception; import javax.servlet.servletexception; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import com.dao.messagedao; import com.dao.impl.messagedaoimpl; import com.vo.message; import com.vo.pagebean; public class showmessageservlet extends httpservlet{ /** * */ private static final long serialversionuid = 6646899131087204214l; @override protected void dopost(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception { doget(req, resp); } @override protected void doget(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception { req.setcharacterencoding("utf-8"); resp.setcontenttype("text/html;charset=utf-8"); int pageindex=0; messagedao md=new messagedaoimpl(); string pageindexstr=req.getparameter("pageindex"); if(pageindexstr!=null){ try{ pageindex=integer.parseint(pageindexstr); } catch (exception e) { } } pagebean<message> pb=new pagebean<message>(pageindex,md.getmessagecount(),10); pb.init(); pb.setlist(md.getmessagelistofpage(pb.getstart(), pb.getend())); req.setattribute("pagebean", pb); req.getrequestdispatcher("index.jsp").forward(req, resp); } }
jsp页面的显示调用
<%@ page language="java" import="java.util.*" pageencoding="utf-8"%> <% string path = request.getcontextpath(); string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/"; %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!doctype html public "-//w3c//dtd html 4.01 transitional//en"> <html> <head> <base href="<%=basepath%>" rel="external nofollow" > <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="styles.css" rel="external nofollow" > --> </head> <c:if test="${empty pagebean}"> <jsp:forward page="showmessage"></jsp:forward> </c:if> <body> <c:foreach var="message" items="${pagebean.list}"> ${message.title } ${message.editdate }<br/> </c:foreach> <a href="showmessage?pageindex=${pagebean.pageindex+1}" rel="external nofollow" >下一个</a>[${pagebean.pageindex }<span>/</span>${pagebean.pagesize}] </body> </html>
总结
以上就是本文关于一个通用的java分页基类代码详解的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他java相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!