分页功能
1.dao 层Hibernate的分页方法
1 package com.hanqi.dao;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import org.hibernate.Session;
7 import org.hibernate.SessionFactory;
8 import org.hibernate.Transaction;
9 import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
10 import org.hibernate.cfg.Configuration;
11 import org.hibernate.service.ServiceRegistry;
12
13 import com.hanqi.entity.Phoner;
14
15 public class PhonerDAO {
16
17 //定义变量
18 private Configuration cfg = null ;
19 private ServiceRegistry sr = null ;
20 private SessionFactory sf = null ;
21 private Session se = null ;
22 Transaction ts = null ;
23 List<Phoner> list = new ArrayList<>() ;
24
25 public PhonerDAO() {
26 //初始化Hibernate
27 cfg = new Configuration().configure() ;
28
29 sr = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build() ;
30
31 }
32
33 //配置加载
34 public void init()
35 {
36 sf = cfg.buildSessionFactory(sr) ;
37
38 se = sf.openSession() ;
39
40 ts = se.beginTransaction() ;
41 }
42
43 //提交事务并释放资源
44 public void destory()
45 {
46 ts.commit() ;
47
48 se.close() ;
49
50 sf.close() ;
51 }
52
53 //分页显示所有联系人
54 public List<Phoner> getAll(int pages)
55 {
56 init() ;
57
58 list = se.createQuery("from Phoner")//HQL语句
59 .setMaxResults(2)//设置每页显示的行数
60 .setFirstResult((pages-1)*2)//设置起始页
61 .list() ;//获得集合
62
63 destory();
64
65 return list ;
66 }
67
68
69 }
2在service调用上述方法并传递一个页码参数
1 package com.hanqi.service;
2
3 import java.util.List;
4
5 import com.hanqi.dao.PhonerDAO;
6 import com.hanqi.entity.Phoner;
7
8 public class PhonerService {
9
10 PhonerDAO pd = new PhonerDAO() ;
11
12 //分页查询
13 public List<Phoner> getAll(int pages)
14 {
15 return pd.getAll(pages) ;
16 }
17
18 }
3底层的代码我们已经写完,接下来就是怎么在网页和struts.xml进行调用并显示数据
这是第一个页面,我们让它通过get方式携带一个页码参数(默认进去就显示第一页)
<%@page import="com.hanqi.service.PhonerService"%>
<%@page import="com.hanqi.entity.Phoner"%>
<%@page import="java.util.List"%>
<%@ 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>
<style type="text/css">
h3{text-shadow: 5px 5px 5px #FFF000 ;}
</style>
</head>
<body>
<%
%>
<!-- 这里设置连接跳转并携带参数传递到下一个页面 -->
<a href="selectPhoner.action?pages=1"><h3>查询联系人</h3></a><br><br>
</body>
</html>
之后点击查询联系人,我们通过struts.xml配置跳转到显示的页面
首先要定义一个Action的类
1 package com.hanqi.action;
2
3 import java.util.List;
4
5 import javax.servlet.http.HttpServletRequest;
6
7 import org.apache.struts2.ServletActionContext;
8
9 import com.hanqi.entity.Phoner;
10 import com.hanqi.service.PhonerService;
11 import com.opensymphony.xwork2.ActionSupport;
12
13 public class PhonerAction {
14
15 PhonerService ps = new PhonerService() ;//实例化
16 private String pages ;
17
18 public String getPages() {
19 return pages;
20 }
21
22 public void setPages(String pages) {
23 this.pages = pages;
24 }
25
26 public String selectPhoner()
27 {
28 String rtn = "fail" ;
29
30 try
31 {
32 HttpServletRequest hsr = ServletActionContext.getRequest() ;//获取原生request
33
34 //获取传递的参数
35 int pages = Integer.parseInt(hsr.getParameter("pages")) ;
36
37 List<Phoner> list = ps.getAll(pages); //调取service层方法
38
39 hsr.setAttribute("pages", pages); //覆盖参数并重新传递回去
40
41 hsr.setAttribute("selectAll", list); //将获取到的数据集合放入请求中
42
43 rtn = "success" ;
44
45 }catch(Exception e)
46 {
47 e.getStackTrace() ;
48 }
49 return rtn ;
50 }
51 }
struts.xml配置
<action name="selectPhoner" class="com.hanqi.action.PhonerAction" method=" selectPhoner">
<result>/WEB-INF/pages/selectPhoner.jsp</result>
</action>
显示数据的jsp,这里我们做了个小小的判断,因为我们设置的每页两条数据,所以我们得到的集合的长度要么0,要么1,要么2所以我们可以判断页码的超出范围
并控制弹窗,这里又做了定时跳转

1 <%@page import="com.hanqi.dao.PhonerDAO"%>
2 <%@page import="com.hanqi.service.PhonerService"%>
3 <%@page import="com.hanqi.entity.Phoner"%>
4 <%@page import="java.util.List"%>
5 <%@ page language="java" contentType="text/html; charset=UTF-8"
6 pageEncoding="UTF-8"%>
7 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
8 <html>
9 <head>
10 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
11 <title>Insert title here</title>
12 <style type="text/css">
13 *{
14 margin:0 ;
15 padding:0;
16 }
17 table
18 {
19 height:150px;
20 width:600px;
21 border:#990 1px solid;
22 text-align:center;
23 margin:0 ;
24 border-radius:8px;
25 }
26 table tr
27 {
28 margin:0 ;
29 border:#990 1px solid;
30 cellpadding:0 ;
31 cellspacing:0 ;
32 }
33 table th
34 {
35 margin:0 ;
36 border:#990 1px solid;
37 cellpadding:0 ;
38 border:1 ;
39 }
40 table td
41 {
42 margin:0 ;
43 border:#990 1px solid;
44 cellpadding:0 ;
45 border:1 ;
46 }
47 </style>
48 </head>
49 <body>
50 <%
51
52 //定义变量,获取父级网页传递的参数
53 int pages = Integer.parseInt(request.getParameter("pages")) ;
54
55 //定义Phoner集合,并将从请求中获取到的集合赋给此集合
56 List<Phoner> list = (List<Phoner>)request.getAttribute("selectAll") ;
57
58 //获取记录条数
59 List<Phoner> list1 = new PhonerDAO().getCount() ;
60
61 int account = list1.size() ;
62
63 int yeshu = 0 ;
64
65 if(account%2 == 0)
66 {
67 yeshu = account/2 ;
68 }
69 else
70 {
71 yeshu = (int)account/2 + 1 ;
72 }
73 %>
74
75 <table cellspacing="0" cellpadding="0">
76
77 <tr>
78 <th>序号</th>
79 <th>姓名</th>
80 <th>电话</th>
81 <th>地址</th>
82 <th>备注</th>
83 </tr>
84 <%
85 //在表格中遍历集合
86 for(Phoner p : list)
87 {%>
88 <tr>
89 <td><%=p.getXuhao() %></td>
90 <td><%=p.getName() %></td>
91 <td><%=p.getTelnum() %></td>
92 <td><%=p.getAddress() %></td>
93 <td><%=p.getRemart() %></td>
94 </tr>
95 <%
96 }
97 %>
98 </table>
99 <%
100 if(list.size() == 0)
101 {%>
102 <script type='text/javascript'>alert('页码超出范围')</script>
103 <%
104 response.setHeader("refresh", "0;url=selectPhoner.action?pages=1");}
105 %>
106 <br>
107 共 <%=yeshu %> 页 <%=account %> 条记录 当前为 <%=pages %> 页
108
109 <a href="selectPhoner.action?pages=<%=pages-1 %>" >上一页</a>
110 <a href="selectPhoner.action?pages=<%=pages+1 %>" >下一页</a>
111 <form action="tiaoPhoner.action" method="post">
112 跳到<input type="text" name="pages">页 <input type="submit" value="跳转" >
113 </form>
114 </body>
115 </html>
这时我们需要的分页就实现了
接下来就是输入页码跳转
首先还是上面的jsp页面,我们加入跳转的代码,通过form表单进行提交数据

dao层的方法还是没有变直接调用就好,至于传递的页码参数则为我们输入的参数,怎么获取呢?
先说struts.xml
1 <action name="tiaoPhoner" class="com.hanqi.action.PhonerAction" method="tiaoPhoner"> 2 3 <result>/WEB-INF/pages/selectPhoner.jsp</result> 4 5 </action>
接下来我们在Action类中定义该方法
1 public String tiaoPhoner()
2 {
3 String rtn = "fail" ;
4
5 try
6 {
7 HttpServletRequest hsr = ServletActionContext.getRequest() ;//获取原生request
8
9 //获取传递的参数
10 try
11 {
12 int page = Integer.parseInt(pages) ;
13
14 System.out.println(page);
15 List<Phoner> list = ps.getAll(page); //调取service层方法
16
17 hsr.setAttribute("pages", page); //覆盖参数并重新传递回去
18
19 hsr.setAttribute("selectAll", list); //将获取到的数据集合放入请求中
20
21 }catch(Exception e)
22 {
23 e.getStackTrace();
24 }
25 rtn = "success" ;
26
27 }catch(Exception e)
28 {
29 e.getStackTrace() ;
30 }
31 return rtn ;
32 }
service层的方法并没有变,这时我们就可以进行页面跳转了
预览图:

这里的共多少页是通过获取总记录条数count计算出来的
因为每页显示两条,所以我们通过判断count%2 == 0来得到总的页数(==0 页数为count/2,否则页数为(int)count/2+1)
查看页面条数的dao层方法
1 //查询有多少条记录
2 public List<Phoner> getCount()
3 {
4 init() ;
5
6 list = se.createQuery("from Phoner").list() ;
7
8 destory();
9
10 return list;
11 }
然后直接在页面调用方法获取集合,集合长度就是我们要用到的count


浙公网安备 33010602011771号