Pager-taglib 2.0 是一套分页标签库,可以灵活地实现多种不同风格的分页导航页面,并且可以很好的与服务器分页逻辑分离。这样一套标签库,你想你的分页效果怎样就能怎样。 pager-taglib,可以方便的帮我们完成分页的功能,当然大家不要担心他的效率,它支持每次从数据库中只取出要显示的数据,这样可以减少数据库的压力。
导入jar包
导入pager-taglib.jar文件,并进行buid path
DAO层代码
在BaseDao类中新建getListForPage方法,可以通过hibernate来解决分页问题
/**
* 使用hql 语句进行操作
* @param hql 需要执行的hql语句
* @param offset 设置开始位置
* @param length 设置读取数据的记录条数
* @return List 返回所需要的集合
*/
public List<T> getListForPage(String hql,int offset,int pageSize) {
Session session = this.sessionFactory.getCurrentSession();
Query query = session.createQuery(hql);
query.setFirstResult(offset);
query.setMaxResults(pageSize);
List list = query.list();
return list;
}
#Service层代码
server层引用步骤2中DAO层的getListForPage方法,并且新建find方法调用getListForPage()
public Pager<User> find() {
Pager<User> pagers = new Pager<User>();
Long total = (Long) this.uniqueResult("select count(*) from User");
int pagesize = SystemContext.getPageSize();
int offset = SystemContext.getOffset();
pagers.setOffset(offset);
pagers.setSize(pagesize);
pagers.setTotal(total);
pagers.setDatas(this.getListForPage("from User",offset,pagesize));
return pagers;
}
springmvc的action中创建list()
/**
* 查询所有的user用户
* @param model
* @return
*/
@RequestMapping(value={"users","/"},method=RequestMethod.GET)
public String list(Model model){
Pager<User> pagers = userService.find();
model.addAttribute("pagers",pagers);
return "user/list";
}
创建SystemContext.java类,存入offset和pagesize两个变量
package cn.xiaoyu.springmvc.domain;
/**
* offset和pagesize
* 比较好的做法是在系统变量类中,将其设置到ThreadLocal变量中
* 这里将其封装到了SystemContext类中
* @author xiaoyu 2015-11-20
*/
public class SystemContext {
private static ThreadLocal<Integer> offset = new ThreadLocal<Integer>();
private static ThreadLocal<Integer> pagesize = 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 getPageSize() {
return pagesize.get();
}
public static void setPageSize(Integer _pagesize) {
pagesize.set(_pagesize);
}
public static void removePageSize() {
pagesize.remove();
}
}
创建拦截器
创建PagerFilter.java拦截器,对上面步骤中的offset和pagesize的两个变量进行拦截并存入SystemContext类中
package cn.xiaoyu.springmvc.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import cn.xiaoyu.springmvc.domain.SystemContext;
public class PagerFilter implements Filter {
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
try {
SystemContext.setOffset(getOffSet(httpRequest));
SystemContext.setPageSize(getPageSize(httpRequest));
chain.doFilter(request, response);
}finally {
SystemContext.removeOffset();
SystemContext.removePageSize();
}
}
protected int getOffSet(HttpServletRequest request) {
int offset = 0;
try {
offset = Integer.parseInt(request.getParameter("pager.offset"));
} catch (NumberFormatException ignore) {
}
return offset;
}
protected int getPageSize(HttpServletRequest request) {
return 10;
}
public void init(FilterConfig config) throws ServletException {
}
public void destroy() {
}
}
web.xml配置拦截器
<!-- 配置pager-lib分页的拦截器 -->
<filter>
<filter-name>pagerFilter</filter-name>
<filter-class>cn.xiaoyu.springmvc.filter.PagerFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>pagerFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
添加分页角标页面
在WebRoot下面添加WebRoot/inc/pager.jsp文件,作为分页的角标
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="pg" uri="http://jsptags.com/tags/navigation/pager"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<pg:pager maxPageItems="10" items="${param.items }" export="curPage=pageNumber" url="${param.url }">
<c:forEach items="${param.params }" var="p">
<pg:param name="${p }"/>
</c:forEach>
<pg:last>
共${param.items }记录,共${pageNumber }页,
</pg:last>
当前第${curPage }页
<pg:first>
<a href="${pageUrl }">首页</a>
</pg:first>
<pg:prev>
<a href="${pageUrl }">上一页</a>
</pg:prev>
<pg:pages>
<c:if test="${curPage eq pageNumber }">
[${pageNumber }]
</c:if>
<c:if test="${curPage ne pageNumber }">
<a href="${pageUrl }">${pageNumber }</a>
</c:if>
</pg:pages>
<pg:next>
<a href="${pageUrl }">下一页</a>
</pg:next>
<pg:last>
<a href="${pageUrl }">尾页</a>
</pg:last>
</pg:pager>
创建分页页面
创建引入pager.jsp的页面list.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">
<title>用户列表</title>
</head>
<body>
<table width="700" align="center" border="1">
<tr>
<td>用户标识</td><td>用户名</td><td>用户昵称</td><td>用户密码</td><td>用户邮箱</td>
<td>操作</td>
</tr>
<c:if test="${pagers.total le 0 }">
<tr>
<td colspan="6">目前还没有用户数据</td>
</tr>
</c:if>
<c:if test="${pagers.total gt 0}">
<c:forEach items="${pagers.datas }" var="u">
<tr>
<td>${u.id }</td><td>${u.username }</td>
<td><a href="${u.id }">${u.nickname }</a></td>
<td>${u.password }</td><td>${u.email }</td>
<td><a href="${u.id }/update">更新</a> <a href="${u.id }/delete">删除</a></td>
</tr>
</c:forEach>
<tr>
<td colspan="6">
<jsp:include page="/inc/pager.jsp">
<jsp:param value="users" name="url"/>
<jsp:param value="${pagers.total}" name="items"/>
</jsp:include>
</td>
</tr>
</c:if>
</table>
</body>
</html>
效果图
如此,就可访问得到如下的效果(表示jsp分页效果实现)