1、分页显示中必须有两个参数,总记录数、记录列表,封装到PagerModel类中
PagerModel.java
1
package com.oa;
2
3
import java.util.List;
4
5
import com.oa.models.Organization;
6
7
/** *//**
8
*
9
* @author Administrator 分页模型,返回记录总数和相应记录
10
*/
11
public class PagerModel
{
12
private int total;
13
private List<Organization> list;
14
15
public int getTotal()
{
16
return total;
17
}
18
19
public void setTotal(int total)
{
20
this.total = total;
21
}
22
23
public List<Organization> getList()
{
24
return list;
25
}
26
27
public void setList(List<Organization> list)
{
28
this.list = list;
29
}
30
}
2、显示页showorgs.jsp代码如下:
1
<%
@ page language="java" contentType="text/html; charset=GB18030"
2
pageEncoding="GB18030"%>
3
<%
@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
4
<%
@ taglib prefix="pg" uri="http://jsptags.com/tags/navigation/pager"%>
5
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
6
<html>
7
<head>
8
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
9
<title>Insert title here</title>
10
</head>
11
<body>
12
<table>
13
<tr>
14
<td>
15
序号
16
</td>
17
<td>
18
机构名称
19
</td>
20
<td>
21
机构编号
22
</td>
23
<td>
24
父机构名称
25
</td>
26
<td>
27
相关操作
28
</td>
29
</tr>
30
<c:if test="${!empty pagerModel.list}">
31
<c:forEach items="${pagerModel.list}" var="org">
32
<tr>
33
<td>
34
${org.orgid }
35
</td>
36
<td>
37
${org.orgname }
38
</td>
39
<td>
40
${org.orgsn }
41
</td>
42
<td>
43
${org.parentorg.orgname }
44
</td>
45
<td>
46
<a href="#">删除</a>
47
</td>
48
</tr>
49
</c:forEach>
50
</c:if>
51
<c:if test="${empty pagerModel.list}">
52
<tr>
53
<td colspan="5">
54
没有找到相应的记录
55
</td>
56
</tr>
57
</c:if>
58
</table>
59
<pg:pager items="${pagerModel.total}" url="organization.action"
60
export="currentPageNumber=pageNumber" maxIndexPages="5"
61
maxPageItems="8">
62
<pg:param name="parentorgid" />
63
<pg:first>
64
<a href="${pageUrl }">首页</a>
65
</pg:first>
66
<pg:prev>
67
<a href="${pageUrl }">前一页</a>
68
</pg:prev>
69
<pg:pages>
70
<c:choose>
71
<c:when test="${currentPageNumber eq pageNumber}">
72
<font color="red">${pageNumber}</font>
73
</c:when>
74
<c:otherwise>
75
<a href="${pageUrl }">${pageNumber }</a>
76
</c:otherwise>
77
</c:choose>
78
</pg:pages>
79
<pg:next>
80
<a href="${pageUrl }">后一页</a>
81
</pg:next>
82
<pg:last>
83
<a href="${pageUrl }">尾页</a>
84
</pg:last>
85
</pg:pager>
86
</body>
87
</html>
本页代码只对对应的记录做分页显示,并未对数据的操作做处理。
url::分页的链接根地址,pager标签会在这个链接的基础上附加分页参数
items:总记录数,pager标签正是根据这个值来计算分页参数的
maxPageItems:每页显示的行数,默认为10
maxIndexPages:在循环输出页码的时候,最大输出多少个页码,默认是10
<pg:param>这个标签很有用,用来设置将要加入到URL的参数。使用Name属性指定即可。
<pg:prev> , <pg:pages>,<pg:next>这些标签都是导航标签顾名思义
3、Action的实现
OrganizationAction.java
1
package com.oa.actions;
2
3
import org.apache.struts2.ServletActionContext;
4
5
import com.oa.PagerModel;
6
import com.oa.managers.OrganizationManager;
7
import com.opensymphony.xwork2.ActionSupport;
8
9
@SuppressWarnings("serial")
10
public class OrganizationAction extends ActionSupport
{
11
12
private OrganizationManager organizationManager;
13
14
private int parentorgid;
15
16
private PagerModel pagerModel;
17
18
public int getParentorgid()
{
19
return parentorgid;
20
}
21
22
public void setParentorgid(int parentorgid)
{
23
this.parentorgid = parentorgid;
24
}
25
26
public void setOrganizationManager(OrganizationManager organizationManager)
{
27
this.organizationManager = organizationManager;
28
}
29
30
public PagerModel getPagerModel()
{
31
return pagerModel;
32
}
33
34
public void setPagerModel(PagerModel pagerModel)
{
35
this.pagerModel = pagerModel;
36
}
37
38
@Override
39
public String execute() throws Exception
{
40
/**//*
41
* int offset = 0;
42
*
43
* try { offset = Integer.parseInt(ServletActionContext.getRequest()
44
* .getParameter("pager.offset")); } catch (RuntimeException e) { //
45
* TODO Auto-generated catch block e.printStackTrace(); } int pagesize =
46
* 8;
47
*/
48
System.out.println("parentorgid:"+parentorgid);
49
pagerModel = organizationManager.findOrgs(parentorgid);// ,
50
// offset,pagesize);
51
ServletActionContext.getRequest()
52
.setAttribute("pagerModel", pagerModel);
53
ServletActionContext.getRequest().setAttribute("parentorgid",
54
parentorgid);
55
return SUCCESS;
56
}
57
58
}
AbstractorPagerManager.java
1
package com.oa.managers.impl;
2
3
import java.util.List;
4
5
import org.hibernate.Query;
6
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
7
8
import com.oa.PagerModel;
9
import com.oa.SystemContext;
10
import com.oa.models.Organization;
11
12
public class AbstractorPagerManager extends HibernateDaoSupport
{
13
@SuppressWarnings("unchecked")
14
public PagerModel searchPaginated(String hql)
{
15
return this.searchPaginated(hql, null, SystemContext.getOffset(), SystemContext.getPagesize());
16
}
17
public PagerModel searchPaginated(String hql, Object obj)
{
18
return this.searchPaginated(hql, new Object[]
{ obj }, SystemContext.getOffset(), SystemContext.getPagesize());
19
}
20
public PagerModel searchPaginated(String hql, Object[] params)
{
21
return this.searchPaginated(hql, params, SystemContext.getOffset(), SystemContext.getPagesize());
22
}
23
public PagerModel searchPaginated(String hql, int offset, int pagesize)
{
24
return this.searchPaginated(hql, null, offset, pagesize);
25
}
26
27
public PagerModel searchPaginated(String hql, Object obj, int offset,
28
int pagesize)
{
29
return this
30
.searchPaginated(hql, new Object[]
{ obj }, offset, pagesize);
31
}
32
33
@SuppressWarnings("unchecked")
34
public PagerModel searchPaginated(String hql, Object[] params, int offset,
35
int pagesize)
{
36
// 查询总的记录数
37
String countHql = this.getCountHql(hql);
38
Query query = this.getSession().createQuery(countHql);
39
if (query != null && params != null && params.length > 0)
{
40
for (int i = 0; i < params.length; i++)
{
41
query.setParameter(i, params[i]);
42
}
43
}
44
int total = ((Long) query.uniqueResult()).intValue();
45
46
// 查询机构列表
47
48
query = this.getSession().createQuery(hql);
49
if (query != null && params != null && params.length > 0)
{
50
for (int i = 0; i < params.length; i++)
{
51
query.setParameter(i, params[i]);
52
}
53
}
54
query.setFirstResult(offset);
55
query.setMaxResults(pagesize);
56
List<Organization> list = query.list();
57
58
PagerModel pagerModel = new PagerModel();
59
pagerModel.setTotal(total);
60
pagerModel.setList(list);
61
return pagerModel;
62
63
}
64
65
private String getCountHql(String hql)
{
66
int index = hql.indexOf("from");
67
if (index != -1)
{
68
return "select count(*) " + hql.substring(index);
69
}
70
throw new RuntimeException("不合法的HQL语句!");
71
}
72
73
}
OrganizationManagerImpl.java
1
package com.oa.managers.impl;
2
3
import com.oa.PagerModel;
4
import com.oa.managers.OrganizationManager;
5
import com.oa.models.Organization;
6
7
public class OrganizationManagerImpl extends AbstractorPagerManager implements
8
OrganizationManager
{
9
10
public void addOrg(Organization org, int parentorgid)
{
11
12
if (parentorgid != 0)
{
13
org.setParentorg((Organization) this.getHibernateTemplate().load(
14
Organization.class, parentorgid));
15
}
16
17
this.getHibernateTemplate().save(org);
18
19
// TODO 设置Organization的唯一编号
20
org.setOrgsn(org.getParentorg() == null ? ("" + org.getOrgid()) : (org
21
.getParentorg().getOrgsn()
22
+ "_" + org.getOrgid()));
23
24
this.getHibernateTemplate().update(org);
25
}
26
27
public void deleteOrg(int orgid)
{
28
this.getHibernateTemplate().delete(
29
this.getHibernateTemplate().load(Organization.class, orgid));
30
31
}
32
33
public Organization findOrg(int orgid)
{
34
return (Organization) this.getHibernateTemplate().load(
35
Organization.class, orgid);
36
}
37
38
public void modifyOrg(Organization org, int parentorgid)
{
39
if (parentorgid != 0)
{
40
org.setParentorg((Organization) this.getHibernateTemplate().load(
41
Organization.class, parentorgid));
42
}
43
44
this.getHibernateTemplate().update(org);
45
}
46
47
@SuppressWarnings("unchecked")
48
public PagerModel findOrgs(int parentorgid)
{// , int offset, int
49
// pagesize) {
50
/**//*
51
* // 查询总的记录数 String selectCountHql = "select count(*) from Organization
52
* org where org.parentorg is null"; if (parentorgid != 0) {
53
* selectCountHql = "select count(*) from Organization org where
54
* org.parentorg.orgid=" + parentorgid; } int total = ((Long)
55
* this.getSession().createQuery(selectCountHql)
56
* .uniqueResult()).intValue(); // 查询机构列表 String selectHql = "from
57
* Organization org where org.parentorg is null"; if (parentorgid != 0) {
58
* selectHql = "from Organization org where org.parentorg.orgid=" +
59
* parentorgid; } List<Organization> list =
60
* this.getSession().createQuery(selectHql)
61
* .setFirstResult(offset).setMaxResults(pagesize).list(); PagerModel
62
* pagerModel = new PagerModel(); pagerModel.setTotal(total);
63
* pagerModel.setList(list); return pagerModel;
64
*/
65
66
if (parentorgid == 0)
{
67
/**//*
68
* return this.searchPaginated( "from Organization org where
69
* org.parentorg is null", offset, pagesize);
70
*/
71
return this
72
.searchPaginated("from Organization org where org.parentorg is null");
73
}
74
/**//*
75
* return this.searchPaginated( "from Organization org where
76
* org.parentorg.orgid=?", parentorgid, offset, pagesize);
77
*/
78
79
return this.searchPaginated(
80
"from Organization org where org.parentorg.orgid=?",
81
parentorgid);
82
}
83
}
SystemContext.java
1
package com.oa;
2
3
public class SystemContext
{
4
public static ThreadLocal<Integer> offset = new ThreadLocal<Integer>();
5
public static ThreadLocal<Integer> pagesize = new ThreadLocal<Integer>();
6
7
public static void setOffset(int offsetValue)
{
8
offset.set(offsetValue);
9
}
10
11
public static int getOffset()
{
12
Integer os = offset.get();
13
if (os == null)
{
14
return 0;
15
}
16
return os;
17
}
18
public static void removeOffset()
{
19
offset.remove();
20
}
21
22
public static void setPagesize(int pagesizeValue)
{
23
pagesize.set(pagesizeValue);
24
}
25
26
public static int getPagesize()
{
27
Integer ps = pagesize.get();
28
if (ps == null)
{
29
return Integer.MAX_VALUE;
30
}
31
return ps;
32
}
33
public static void removePagesize()
{
34
pagesize.remove();
35
}
36
}
关于Action的配置比较简单,在此不再作介绍。
4、实现并配置过滤器
PagerFilter.java
1
package com.oa.filters;
2
3
import java.io.IOException;
4
5
import javax.servlet.Filter;
6
import javax.servlet.FilterChain;
7
import javax.servlet.FilterConfig;
8
import javax.servlet.ServletException;
9
import javax.servlet.ServletRequest;
10
import javax.servlet.ServletResponse;
11
import javax.servlet.http.HttpServletRequest;
12
13
import com.oa.SystemContext;
14
15
public class PagerFilter implements Filter
{
16
17
public void destroy()
{
18
19
}
20
21
public void doFilter(ServletRequest request, ServletResponse response,
22
FilterChain chain) throws IOException, ServletException
{
23
24
HttpServletRequest myrequest = (HttpServletRequest) request;
25
SystemContext.setOffset(this.getOffset(myrequest));
26
SystemContext.setPagesize(this.getPagesize());
27
try
{
28
chain.doFilter(request, response);
29
} catch (Exception e)
{
30
31
} finally
{
32
SystemContext.removeOffset();
33
SystemContext.removePagesize();
34
}
35
}
36
37
protected int getOffset(HttpServletRequest request)
{
38
int offset = 0;
39
try
{
40
offset = Integer.parseInt(request.getParameter("pager.offset"));
41
} catch (NumberFormatException e)
{
42
// TODO Auto-generated catch block
43
System.out.println("--------------------");
44
//e.printStackTrace();
45
}
46
return offset;
47
}
48
49
protected int getPagesize()
{
50
return 8;
51
}
52
53
public void init(FilterConfig arg0) throws ServletException
{
54
55
}
56
57
}
在web.xml中配置过滤器,必须放在struts2之前,否则有可能不起作用
<filter>
<filter-name>pagerFilter</filter-name>
<filter-class>com.oa.filters.PagerFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>pagerFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>