分页模板
1、首先建立一个工具包msq.utils
新建两个类
NavigationTag.java
package msq.utils;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
/**
* 显示格式:首页 上一页 1 2 3 4 5下一页 尾页
*/
public class NavigationTag extends TagSupport {
static final long serialVersionUID = 2372405317744358833L;
private String bean = "page";//request 中用于保存Page<E> 对象的变量名,默认为“page”
private String url = null;//分页跳转的url地址,此属性必须
private int number = 5;//显示页码数量
private int goPage = 1;//显示要跳转到的页码
@Override
public int doStartTag() throws JspException {
JspWriter writer = pageContext.getOut();
HttpServletRequest request =(HttpServletRequest) pageContext.getRequest();
Page page = (Page) request.getAttribute(bean);
if (page == null)return SKIP_BODY;
url = resolveUrl(url, pageContext);
try {
// 计算总页数
int pageCount = page.getTotalRecordCount() / page.getPageSize();
if (page.getTotalRecordCount() % page.getPageSize() > 0) {
pageCount++;
}
writer.print("<nav><ul class=\"pagination\">");
//首页链接路径
String homeUrl = append(url, "currentPage", 1);
homeUrl = append(homeUrl, "pageSize", page.getPageSize());
//末页链接路径
String backUrl = append(url, "currentPage", pageCount);
backUrl = append(backUrl, "pageSize", page.getPageSize());
// 显示“首页 上一页”按钮
if (page.getCurrentPage() > 1) {
String preUrl = append(url, "currentPage", page.getCurrentPage() - 1);
preUrl = append(preUrl, "pageSize", page.getPageSize());
writer.print("<li><a href=\"" + homeUrl + "\">" + "首页</a></li>");
writer.print("<li><a href=\"" + preUrl + "\">" + "上一页</a></li>");
}
else {
writer.print("<li class=\"disabled\"><a href=\"#\">" + "首页 </a></li>");
writer.print("<li class=\"disabled\"><a href=\"#\">" + "上一页 </a></li>");
}
// 显示当前页码的前2页码和后两页码
// 若1 则 1 2 3 4 5, 若2 则 1 2 3 4 5, 若3 则1 2 3 4 5,
// 若4 则 2 3 4 5 6 ,若10 则 8 9 10 11 12
int number2=number/2; //总共显示number页,分为前后各一半
int indexPage =1;
if(page.getCurrentPage() - number2 <=0){ //处理前几页
indexPage=1; //起始页码位置
}else if(pageCount-page.getCurrentPage() <=number2){ //处理尾几页
indexPage=pageCount-number+1;//从最后向前number-1页
if(indexPage<=0)indexPage=1; //防止出现负数页码
}else{
indexPage= page.getCurrentPage() - number2; //中间页码
}
for (int i= 1;i <= number && indexPage <= pageCount;indexPage++,i++){
if (indexPage == page.getCurrentPage()) {
writer.print("<li class=\"active\"><a href=\"#\">" + indexPage+"<span class=\"sr-only\"></span></a></li>");
continue;
}
String pageUrl = append(url, "currentPage", indexPage);
pageUrl = append(pageUrl, "pageSize", page.getPageSize());
writer.print("<li><a href=\"" + pageUrl + "\">" + indexPage + "</a></li>");
}
// 显示“下一页”按钮
if (page.getCurrentPage() < pageCount) {
String nextUrl = append(url, "currentPage", page.getCurrentPage() + 1);
nextUrl = append(nextUrl, "pageSize", page.getPageSize());
writer.print("<li><a href=\"" + nextUrl + "\">" + "下一页</a></li>");
writer.print("<li><a href=\"" + backUrl + "\">" + "尾页</a></li>");
} else {
writer.print("<li class=\"disabled\"><a href=\"#\">" + "下一页</a></li>");
writer.print("<li class=\"disabled\"><a href=\"#\">" + "尾页</a></li>");
}
writer.print("<li class=\"disabled\"><a href='javascript:void(0)'>共" + pageCount + "页" + page.getTotalRecordCount() + "条记录</a></li>");
writer.print("</nav>");
} catch (IOException e) {
e.printStackTrace();
}
return SKIP_BODY;
}
private String append(String url, String key, int value) {
return append(url, key, String.valueOf(value));
}
/**
* 为url 添加参数对儿
*/
private String append(String url, String key, String value) {
if (url == null || url.trim().length() == 0) {
return "";
}
if (url.indexOf("?") == -1) {
url = url + "?" + key + "=" + value;
} else {
if (url.endsWith("?")) {
url = url + key + "=" + value;
} else {
url = url + "&" + key + "=" + value;
}
}
return url;
}
/**
* 为url 添加翻页请求参数
*/
private String resolveUrl(String url,
javax.servlet.jsp.PageContext pageContext) throws JspException {
Map params = pageContext.getRequest().getParameterMap();
for (Object key : params.keySet()) {
if ("currentPage".equals(key) || "pageSize".equals(key)){
continue;
}
Object value = params.get(key);
if (value == null){
continue;
}
if (value.getClass().isArray()) {
url = append(url, key.toString(), ((String[]) value)[0]);
} else if (value instanceof String) {
url = append(url, key.toString(), value.toString());
}
}
return url;
}
public void setUrl(String url) {
this.url = url;
}
public void setNumber(int number) {
this.number = number;
}
}
Page.java
package msq.utils;
import java.util.List;
public class Page<T> {
private Integer totalRecordCount;//总记录数
private Integer pageSize;//一页几条记录
private Integer currentPage; //当前页号
private List<T> list;//当前页的记录list,传递给页面
public Integer getTotalRecordCount() {
return totalRecordCount;
}
public void setTotalRecordCount(Integer total) {
this.totalRecordCount = total;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer size) {
this.pageSize = size;
}
public Integer getCurrentPage() {
return currentPage;
}
public void setCurrentPage(Integer page) {
this.currentPage = page;
}
public List<T> getList() {
return list;
}
public void setList(List<T> rows) {
this.list = rows;
}
}
2、在工程的WebRoot/WEB-INF下新建标签文件tle
commons.tld
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<!-- 指定标签库的版本号 -->
<tlib-version>1.0</tlib-version>
<!-- 指定JSP的版本号 -->
<jsp-version>1.2</jsp-version>
<!-- 指定标签库的名称 -->
<short-name>common</short-name>
<!-- 指定标签库的URI -->
<uri>http://www.nynu.edu.cn/page</uri>
<!-- 指定标签库的显示名称 -->
<display-name>Common Tag</display-name>
<!-- 指定标签库的描述 -->
<description>Common Tag library</description>
<!-- 注册一个自定义标签 -->
<tag>
<!-- 指定注册的自定义标签名称 -->
<name>page</name>
<!-- 指定自定义标签的标签处理器类 -->
<tag-class>msq.utils.NavigationTag</tag-class>
<!-- 指定标签体类型 -->
<body-content>JSP</body-content>
<!-- 描述 -->
<description>create navigation for paging</description>
<!-- 指定标签中的属性 -->
<attribute>
<!-- 指定属性名称 -->
<name>url</name>
<!-- 该属性为true时表示其指定是属性为必须属性 -->
<required>true</required>
<!-- 该属性用于指定能不能使用表达式来动态指定数据,为true时表示可以 -->
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>number</name>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
3、在需要加分页效果的页面前加标签注解
<%@ taglib prefix="mypage" uri="http://www.nynu.edu.cn/page" %>
4、在合适的位置放上分页效果图
url="与后台连接到页面地址"
<mypage:page url="#" ></mypage:page>
5、新建一个类CustomPo,当前类继承CustomPo类
package msq.po;
public class CustomPo {
Integer start;
Integer pageSize;
public Integer getStart() {
return start;
}
public void setStart(Integer start) {
this.start = start;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
}
6、在后台的查找类中依次加上两个参数
controller包
@RequestParam(name="currentPage",defaultValue="1")Integer currentPage,//起始页
@RequestParam(name="PageSize",defaultValue="10")Integer PageSize,//页大小
将原先用来储存数据的List<T> 改成 Page<T>
在查询的基础上分页只需要再加一个参数即可
7、更改对应的sql语句 ,加上限制条件
<!-- 查找的分页 -->
<sql id="sqlLimit">
<if test="start!=null and pageSize!=null">
limit #{start},#{pageSize}
</if>
</sql>