服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - JAVA教程 - 一个通用的Java分页基类代码详解

一个通用的Java分页基类代码详解

2021-03-08 13:13wgd7 JAVA教程

这篇文章主要介绍了一个通用的Java分页基类代码详解,具有一定借鉴价值,需要的朋友可以参考下。

分页的基类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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调用

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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页面的显示调用

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<%@ 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相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

原文链接:http://www.open-open.com/code/view/1426342709482

延伸 · 阅读

精彩推荐