Hibernate+pager-taglib实现分页功能
1.导入pager-taglib.jar包;
![](https://images2015.cnblogs.com/blog/1163222/201705/1163222-20170517165800494-815940375.jpg)
2.引入pager-taglib标签:
<%@ taglib prefix="pg" uri="http://jsptags.com/tags/navigation/pager" %>
3.建立PagerModel类:
public class PagerModel<T> {
private int offset;
private int pageSize;
private long totalSize;
private List<T> dates;
public int getOffset() {
return offset;
}
public void setOffset(int offset) {
this.offset = offset;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public long getTotalSize() {
return totalSize;
}
public void setTotalSize(long totalSize) {
this.totalSize = totalSize;
}
public List<T> getDates() {
return dates;
}
public void setDates(List<T> dates) {
this.dates = dates;
}
}
4.定义offset、pagesize类:
线程局部变量(ThreadLocal)其实的功用非常简单,就是为每一个使用该变量的线程都提供一个变量值的副本,是Java中一种较为特殊的线程绑定机制,
是每一个线程都可以独立地改变自己的副本,而不会和其它线程的副本冲突。
public class SystemContext {
private static ThreadLocal<Integer> offset = new ThreadLocal<Integer>();
private static ThreadLocal<Integer> size = new ThreadLocal<Integer>();
public static Integer getOffset() {
return offset.get();
}
public static void setOffset(Integer _offset) {
offset.set(_offset);
}
public static void removeOffset() {
offset.remove();
}
public static Integer getSize() {
return size.get();
}
public static void setSize(Integer _size) {
size.set(_size);
}
public static void removeSize() {
size.remove();
}
}
5.过滤器中初始化pagesize、offset:
public class PagerFilter implements Filter {
private int pageSize;
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
int offset = 0 ;
try {
offset = Integer.parseInt(request.getParameter("page.offset"));
} catch (NumberFormatException
e) {
}
try {
SystemContext.setOffset(offset);
SystemContext.setPageSize(pageSize);
chain.doFilter(request, response);
} finally {
SystemContext.removeOffset();
SystemContext.removePageSize();
}
}
@Override
public void init(Fi
lterConfig filterConfig) throws ServletException {
try {
pageSize = Integer.parseInt(filterConfig.getInitParameter("pageSize"));
} catch (NumberFormatException e) {
pageSize = 5;
}
}
}
6.配置过滤器web.xml中:
<filter>
<filter-name>PagerFilter</filter-name>
<filter-class>com.ite.smvcDemo.filter.PagerFilter</filter-class>
<init-param>
<param-name>pageSize</param-name>
<param-value>5</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>PagerFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
7.创建公共的page.jsp
重要参数的说明:
Offset:传过来的值是偏移量,是选择的页面的页数。比如你的记录/页pageSize=3,那
么传过来的offset应该如下处理:offset/3+1这样得到的就是要生成的页数!
pg:pager设置分页的总体参数
url:分页的链接根地址,
pager标签会在这个链接的基础上附加分页参数,但不允许像给页面跳转的
url那样,在后面直接添加参数,而参数的传递时需要使用
pg:param来指定。
items:总记录数,
pager标签正是根据这个值来计算分页参数,很重要。
maxPageItems:每页显示的行数,默认为10
maxIndexPages:在循环输出页码的时候,最大输出多少个页码,默认是10
pg:first第一页的标签
pg:pre上一页标签
pg:next下一页标签
pg:last最后一页标签
pg:pages循环输出页码信息对于上面的标签都有类似的export
变量:
pageUrl-分页链接
URL地址(最重要的export参数)
pageNumber-页码
firstItem–对应页第一行的索引值
lastItem-对应页最后一行的索引值
<%@ page pageEncoding="UTF
-
8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="pg" uri="http://jsptags.com/tags/navigation/pager" %>
<%
String url = request.getParameter("url");
int total = Integer.parseInt(request.getParameter("items"));
%>
<pg:page
r url="<%=url %>" items="<%=total %>" maxPageItems="5" maxIndexPages="15"
export="currentPageNumber=pageNumber">
<pg:first>
<a href="${pageUrl }">Home</a>
</pg:first>
<pg:prev>
<a href="${pageUrl }">Previous</a>
</pg:prev>
<pg:pages>
<c:choose>
<c:when test="${currentPageNumber eq pageNumber}">
<font color="red">${pageNumber }</font>
</c:when>
<c:otherwise>
<a href="${pag
eUrl }">${pageNumber }</a>
</c:otherwise>
</c:choose>
</pg:pages>
<pg:next>
<a href="${pageUrl }">Next</a>
</pg:next>
<pg:last>
<a href="${pageUrl }">End</a>
</pg:last>
</pg:pager>
8.在UsersDAOImp类里面增加public PagerModel<UsersVO> findAllByPage()方法:
public class UsersDAOImp extends BaseDAOImp<Users> implements IUsersDAO{
@SuppressWarnings("unchecked")
@Override
public PagerModel<UsersVO> findAllByPage() {
PagerModel
<UsersVO> page = new PagerModel<UsersVO>();
ArrayList<UsersVO> dates = new ArrayList<UsersVO> ();
int offset = SystemContext.getOffset();
int pageSize = SystemContext.getPageSize();
//
使用
hibernate
完成分页
List<Users> tempDates = this.getSession().c
reateQuery(" from Users ")
.setFirstResult(offset)
.setMaxResults(pageSize)
.list();
for(Users users : tempDates){
UsersVO usersVO = new UsersVO();
BeanUtils.copyProperties(users, usersVO);
usersVO.setRoleName(users.getRole().getRoleName());
dates.add(usersVO);
}
page.setDates(dates);
page.setOffset(offset);
page.setPageSize(pageSize);
//
获取总记录数
long totalSize = (long) this.getSession().createQuery("select count(*) fro
m Users
").uniqueResult();
page.setTotalSize(totalSize);
return page;
}
}
controller层
![](https://images2015.cnblogs.com/blog/1163222/201705/1163222-20170517170630182-771150418.jpg)
最后在jsp的table循环中加入
![](https://images2015.cnblogs.com/blog/1163222/201705/1163222-20170517170400775-2080475251.jpg)