JavaWeb开发之分页技术

import java.util.List;

/*
 * 实现分页
 */
public class PageBean {
	//当前页
    private int currentpage;
    //总页数
    private int totalpagenum;
    //单页记录数
    private int pagesize;
    //总记录
    private int totalrecord;
    //起始数据
    private int startindex;
    //结束数据
    private int endindex;
    //存放每页数据的List
    private List<?> pagelist;//当前页的记录
    public PageBean(int currentpage, int pagesize, int totalrecord) {
		this.currentpage = currentpage;
		//总页数计算出来的,不允许外界设置
		if(totalrecord%pagesize>0){
			this.totalpagenum=totalrecord/pagesize+1;	
		}else{
			this.totalpagenum=totalrecord/pagesize;
		}
		this.startindex=(currentpage)*pagesize+1;
		if(startindex+pagesize<totalpagenum){
			this.endindex=startindex+pagesize;
		}else{
			this.endindex=totalpagenum;
		}
		this.pagesize= pagesize;
		this.totalrecord = totalrecord;
		
	}
	public PageBean(){
		
	}
    public void setTotalPageNum(int totalpagenum) {
		totalpagenum = totalpagenum;
	}
	public void setstartindex(int startindex) {
		this.startindex = startindex;
	}
	public void setendindex(int endIndex) {
		this.endindex = endIndex;
	}
	public int getcurrentpage() {
		return currentpage;
	}
	public void setcurrentpage(int currentpage) {
		this.currentpage = currentpage;
	}
	public int gettotalpagenum() {
		if(totalrecord%pagesize>0){
			this.totalpagenum=totalrecord/pagesize+1;	
		}else{
			this.totalpagenum=totalrecord/pagesize;
		}
		return totalpagenum;
	}
	public int getpagesize() {
		return pagesize;
	}
	public void setpagesize(int cols) {
		this.pagesize = cols;
	}
	public int gettotalrecord() {
		return totalrecord;
	}
	public void settotalrecord(int totalrecord) {
		this.totalrecord = totalrecord;
	}
	public int getstartindex() {
		this.startindex=(currentpage-1)*pagesize;
		return startindex;
	}
	public int getendindex() {
		if(startindex+pagesize<totalpagenum){
			this.endindex=startindex+pagesize;
		}else{
			this.endindex=totalpagenum;
		}
		return endindex;
	}
	public List<?> getpagelist() {
		return pagelist;
	}
	public void setpagelist(List<?> pagelist) {
		this.pagelist = pagelist;
	}
}

以上是PageBean类。

/*
	 * 以分页的方式获取数据 Day20
	 */
	public String findAllwithPageBean(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		    PageBean pb=new PageBean();
			String CurrentPage=request.getParameter("CurrentPage");
			String RowSize=request.getParameter("pageSize");
			if(CurrentPage!=null&&!CurrentPage.trim().isEmpty()){	
				pb.setcurrentpage(Integer.parseInt(CurrentPage));
			}else{
				pb.setcurrentpage(1);
			}
			if(RowSize!=null&&!RowSize.trim().isEmpty()){
				pb.setpagesize(Integer.parseInt(RowSize));
			}else{
				pb.setpagesize(10);
			}
			System.out.println("CustomerServlet当前页:"+pb.getcurrentpage());
			System.out.println("CustomerServlet每页数:"+pb.getpagesize());
			pb= new CustomerService().getInfor(pb);
			List l=pb.getpagelist();
	        /*for (t_user t : (List<t_user>)l) {
				System.out.println(t);
			}*/
			request.setAttribute("Pagebean", pb);
			return "f:/Day20userTable.jsp";
	}

以上是Servlet页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="com.aitigo.day20.doman.PageBean" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>用户表单</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">
	-->

  </head>
  <!-- 含有编辑和删除功能 -->
  <body>
<table align="center" border="1" bordercolor="red" cellspacing="0">
          <tr><th>姓名</th><th>性别</th><th>电话号</th><th>生日</th><th>邮箱</th><th>编辑</th></tr>
         <c:forEach step="1" items="${Pagebean.pagelist}" var="user">
            <tr><td>${user.user_name} </td><td>${user.user_sex}</td><td>${user.user_phone}</td><td>${user.user_bir}</td><td>${user.user_email}</td>
            <td><a href="${pageContext.request.contextPath}/CustomerServlet?MethodName=edit&username=${user.user_name}">编辑</a>&nbsp;
            <a href="${pageContext.request.contextPath}/CustomerServlet?MethodName=delete&username=${user.user_name}">删除</a></td></tr>
         </c:forEach>
         </table>
         <div align="center">总共${Pagebean.totalpagenum}页/当前第${Pagebean.currentpage}页&nbsp;<span><a href="${pageContext.request.contextPath}/CustomerServlet?MethodName=findAllwithPageBean&CurrentPage=1">首页</a>
         </span>&nbsp;<span>
         <c:choose >
            <c:when test="${requestScope.Pagebean.currentpage+1<requestScope.Pagebean.totalpagenum}">
            <a href="${pageContext.request.contextPath}/CustomerServlet?MethodName=findAllwithPageBean&CurrentPage=${requestScope.Pagebean.currentpage+1}">下一页</a>
            </c:when>
            <c:otherwise>
            <a href="${pageContext.request.contextPath}/CustomerServlet?MethodName=findAllwithPageBean&CurrentPage=${requestScope.Pagebean.totalpagenum}">下一页</a>
            </c:otherwise>
         </c:choose>
         </span>
         &nbsp;<span><a href="${pageContext.request.contextPath}/CustomerServlet?MethodName=findAllwithPageBean&CurrentPage=${requestScope.Pagebean.totalpagenum}">尾页</a></span></div>
     
  </body>
</html>

注意,在分页中使用el表达式的时候,pageBean的get和set方法必须小写,否则会报错,说属性找不到,在jdk1.6环境下,其他环境不知道

猜你喜欢

转载自blog.csdn.net/qq_37904981/article/details/81428506
今日推荐