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

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

服务器之家 - 编程语言 - Java教程 - java实现分页显示效果

java实现分页显示效果

2021-05-28 11:12魔灵儿 Java教程

这篇文章主要为大家详细介绍了java实现页显示效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了java实现页显示效果的具体代码,供大家参考,具体内容如下

效果图如下:

java实现分页显示效果

实现步骤:

1.创建实体user.class,参考代码如下:

?
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
public class user {
 private string name;
 private int age;
 private string gender;
 
 public string getname() {
 return name;
 }
 
 public void setname(string name) {
 this.name = name;
 }
 
 public int getage() {
 return age;
 }
 
 public void setage(int age) {
 this.age = age;
 }
 
 public string getgender() {
 return gender;
 }
 
 public void setgender(string gender) {
 this.gender = gender;
 }
 
 public user(string name, int age, string gender) {
 super();
 this.name = name;
 this.age = age;
 this.gender = gender;
 }
 
 public user() {
 }
 
}

2.创建分页模型pagebean.class,参考代码如下:

?
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
112
113
114
115
116
public class pagebean<t> {
 
 private int pagenum;
 private int pagesize;
 private int totalrecord;
 private int totalpage;
 private list<t> list;
 private int start;
 private int end;
 private int fromindex;
 private int toindex;
 
 public pagebean(int pagenum, int pagesize, int totalrecord) {
 this.pagenum = pagenum;
 this.pagesize = pagesize;
 this.totalrecord = totalrecord;
 fromindex=(pagenum-1)*pagesize;
 toindex=pagenum*pagesize>totalrecord?totalrecord:pagenum*pagesize;
 
 if (totalrecord % pagesize == 0) {
  this.totalpage = totalrecord / pagesize;
 } else {
  this.totalpage = totalrecord / pagesize + 1;
 }
 start = 1;
 end = 5;
 if (totalpage <= 5) {
  end = this.totalpage;
 } else {
  start = pagenum - 2;
  end = pagenum + 2;
 
  if (start < 1) {
  start = 1;
  end = 5;
  }
  if (end > this.totalpage) {
  end = totalpage;
  start = end - 5;
  }
 }
 }
 
 public int getpagenum() {
 return pagenum;
 }
 
 public void setpagenum(int pagenum) {
 this.pagenum = pagenum;
 }
 
 public int getpagesize() {
 return pagesize;
 }
 
 public void setpagesize(int pagesize) {
 this.pagesize = pagesize;
 }
 
 public int gettotalrecord() {
 return totalrecord;
 }
 
 public int getfromindex() {
 return fromindex;
 }
 
 public void setfromindex(int fromindex) {
 this.fromindex = fromindex;
 }
 
 public int gettoindex() {
 return toindex;
 }
 
 public void settoindex(int toindex) {
 this.toindex = toindex;
 }
 
 public void settotalrecord(int totalrecord) {
 this.totalrecord = totalrecord;
 }
 
 public int gettotalpage() {
 return totalpage;
 }
 
 public void settotalpage(int totalpage) {
 this.totalpage = totalpage;
 }
 
 public list<t> getlist() {
 return list;
 }
 
 public void setlist(list<t> list) {
 this.list = list.sublist(fromindex, toindex);
 }
 
 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;
 }
 
}

3.创建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
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
112
113
114
115
116
117
118
119
120
/**
 *index.jsp
 */
<%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <title>insert title here</title>
  </head>
  <body>
    <a href="${pagecontext.request.contextpath }/main" rel="external nofollow" >分页显示</a>
  </body>
</html>
 
/**
 *main.jsp
 */
<%@ page language="java" contenttype="text/html; charset=utf-8"
 pageencoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <script type="text/javascript"
   src="${pagecontext.request.contextpath}/jquery/jquery.min.js"></script>
    <script type="text/javascript">
   function gos() {
 var pagenum = $.trim($("#pagenum").val());
 if(isnan(pagenum)){
  alert("输入的不是数字 ,请输入数字!");
  return ;
 }
 if(pagenum==""){
  alert("输入为空,请重新输入!");
  return ;
 }
 if(pagenum<1||pagenum>${requestscope.pagebean.totalpage}){
  alert("超出范围,请重新输入!");
  return ;
 }
      location.href="${pagecontext.request.contextpath}/main?pagenum=" rel="external nofollow" +pagenum;
 }
    </script>
    <title>分页显示</title>
  </head>
  <body>
 <center>
 <table width="40%" style="text-align:center">
  <tr>
  <th>姓名</th>
  <th>性别</th>
  <th>年龄</th>
  </tr>
  <c:foreach items="${requestscope.pagebean.list }" var="i">
  <tr>
   <td>${i.name }</td>
   <td>${i.gender }</td>
   <td>${i.age }</td>
  </tr>
  </c:foreach>
 </table>
 </center>
 <br />
 <center>
 <a href="${pagecontext.request.contextpath}/main?pagenum=1" rel="external nofollow" >首页</a>
 <c:if test="${requestscope.pagebean.pagenum ==1}">
 <c:foreach begin="${requestscope.pagebean.start}"
  end="${requestscope.pagebean.end}" var="i">
  <c:if test="${requestscope.pagebean.pagenum == i}">
            ${i}
          </c:if>
  <c:if test="${requestscope.pagebean.pagenum != i}">
  <a href="${pagecontext.request.contextpath}/main?pagenum=${i}" rel="external nofollow" rel="external nofollow" rel="external nofollow" >${i}</a>
  </c:if>
 </c:foreach>
 <a
  href="${pagecontext.request.contextpath}/main?pagenum=${requestscope.pagebean.pagenum+1}" rel="external nofollow" rel="external nofollow" >下一页</a>
 </c:if>
 
 <c:if
 test="${requestscope.pagebean.pagenum > 1 && requestscope.pagebean.pagenum < requestscope.pagebean.totalpage}">
 <a
  href="${pagecontext.request.contextpath}/main?pagenum=${requestscope.pagebean.pagenum-1}" rel="external nofollow" rel="external nofollow" >上一页</a>
 <c:foreach begin="${requestscope.pagebean.start}"
  end="${requestscope.pagebean.end}" var="i">
  <c:if test="${requestscope.pagebean.pagenum == i}">
            ${i}
          </c:if>
  <c:if test="${requestscope.pagebean.pagenum != i}">
  <a href="${pagecontext.request.contextpath}/main?pagenum=${i}" rel="external nofollow" rel="external nofollow" rel="external nofollow" >${i}</a>
  </c:if>
 </c:foreach>
 <a
  href="${pagecontext.request.contextpath}/main?pagenum=${requestscope.pagebean.pagenum+1}" rel="external nofollow" rel="external nofollow" >下一页</a>
 </c:if>
 
 <c:if
 test="${requestscope.pagebean.pagenum == requestscope.pagebean.totalpage}">
 <a
  href="${pagecontext.request.contextpath}/main?pagenum=${requestscope.pagebean.pagenum-1}" rel="external nofollow" rel="external nofollow" >上一页</a>
 <c:foreach begin="${requestscope.pagebean.start}"
  end="${requestscope.pagebean.end}" var="i">
  <c:if test="${requestscope.pagebean.pagenum == i}">
            ${i}
          </c:if>
  <c:if test="${requestscope.pagebean.pagenum != i}">
  <a href="${pagecontext.request.contextpath}/main?pagenum=${i}" rel="external nofollow" rel="external nofollow" rel="external nofollow" >${i}</a>
  </c:if>
 </c:foreach>
   </c:if>
   <a
 href="${pagecontext.request.contextpath}/main?pagenum=${requestscope.pagebean.totalpage}" rel="external nofollow" >尾页</a><br><br>
 跳转到
   <input type="text" id="pagenum" size="1px"></input>页
   <a href="javascript:gos()" rel="external nofollow" >确定</a>, 共[${requestscope.pagebean.totalpage }]页,[${requestscope.pagebean.totalrecord}]条记录
 </center>
  </body>
</html>

4.创建servlets.class,参考代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@webservlet("/main")
public class servlets extends httpservlet {
 
 @override
 protected void service(httpservletrequest request, httpservletresponse response)
  throws servletexception, ioexception {
 string page = request.getparameter("pagenum");
 int pagenum = integer.parseint((page == null ? "1" : page));
 int pagesize = 5;
 service s = new service();
 pagebean<user> pb = s.findall(pagenum, pagesize);
 request.setattribute("pagebean", pb);
 request.getrequestdispatcher("/main.jsp").forward(request, response);
 }
}

5.创建service.class,参考代码如下:

?
1
2
3
4
5
6
7
8
9
10
public class service {
 public pagebean<user> findall(int pagenum, int pagesize) {
 userdao userdao = new userdao();
 list<user> users = userdao.findall();
 int totalrecord = users.size();
 pagebean<user> pb = new pagebean<>(pagenum, pagesize, totalrecord);
 pb.setlist(users);
 return pb;
 }
}

6.创建userdao.class,参考代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
public class userdao {
 list<user> users=new arraylist<>();
 user user;
 public list<user> findall(){
 for(int i=1;i<99;i++){
  user=new user("name-"+i, (int)(100*math.random()), (int)(10*math.random())%2==1?"男":"女");
  users.add(user);
 }
 return users;
 }
}

需注意的问题:

1.需在webcontent下创建文件夹jquery,在他里面放入jquery.min.js这个文件。否则跳转功能异常。

2.index.jsp与main.jsp都在webcontent文件夹下。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq_41815326/article/details/81990529

延伸 · 阅读

精彩推荐