首先将pager-taglib.jar放到lib目录下
在创建一个Pager类,代码如下:
package org.zttc.itat.model;
import java.util.List;
public class Pager<T> {
private List<T> datas;
private int pageOffset;
private long totalRecord;
private int pageSize;
public List<T> getDatas() {
return datas;
}
public void setDatas(List<T> datas) {
this.datas = datas;
}
public int getPageOffset() {
return pageOffset;
}
public void setPageOffset(int pageOffset) {
this.pageOffset = pageOffset;
}
public long getTotalRecord() {
return totalRecord;
}
public void setTotalRecord(long totalRecord) {
this.totalRecord = totalRecord;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
}
分页中的一些参数需要从页面传过来,我们还需要创建一个SystemContext类,当然这个类要使用ThreadLocal模式,具体代码如下:
package org.zttc.itat.model;
public class SystemContext {
private static ThreadLocal<Integer> pageOffset = new ThreadLocal<Integer>();
private static ThreadLocal<Integer> pageSize = new ThreadLocal<Integer>();
public static int getPageOffset() {
return pageOffset.get();
}
public static void setPageOffset(int _pageOffset) {
pageOffset.set(_pageOffset);
}
public static int getPageSize() {
return pageSize.get();
}
public static void setPageSize(int _pageSize) {
pageSize.set(_pageSize);
}
public static void removePageOffset() {
pageOffset.remove();
}
public static void removePageSize() {
pageSize.remove();
}
}
当然我还需要一个SystemContextFilter过滤器了,代码如下:
package org.zttc.itat.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 org.zttc.itat.model.SystemContext;
public class SystemContextFilter implements Filter{
private int pageSize = 0;
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
try {
int pageOffset = 0;
try {
pageOffset = Integer.parseInt(req.getParameter("pager.offset"));
} catch (NumberFormatException e) {}
SystemContext.setPageOffset(pageOffset);
SystemContext.setPageSize(pageSize);
chain.doFilter(req, resp);
} finally {
SystemContext.removePageOffset();
SystemContext.removePageSize();
}
}
@Override
public void init(FilterConfig cfg) throws ServletException {
try {
pageSize = Integer.parseInt(cfg.getInitParameter("pageSize"));
} catch (NumberFormatException e) {
pageSize = 10;
}
}
}
web.xml配置一下:
<filter>
<filter-name>SystemContextFilter</filter-name>
<filter-class>org.zttc.itat.filter.SystemContextFilter</filter-class>
<init-param>
<param-name>pageSize</param-name>
<param-value>15</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SystemContextFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
分页页面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="15" items="${param.items }" export="curPage=pageNumber" url="${param.url }">
<c:forEach items="${param.params }" var="p">
<pg:param name="${p }"/>
</c:forEach>
<pg:last>
共${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>