由于项目需要自己写了个简单的分页标签,只完成了简单的分页功能,没有提供样式和多分页的处理,下面是代码的DEMO:
PagerTag.java
package lectery.tag;
import java.io.IOException;
import javax.servlet.jsp.JspContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class PagerTag extends SimpleTagSupport {
private String url;
private String page;
private String pageCount;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getPage() {
return page;
}
public void setPage(String page) {
this.page = page;
}
public String getPageCount() {
return pageCount;
}
public void setPageCount(String pageCount) {
this.pageCount = pageCount;
}
public void doTag() throws IOException,JspException{
JspContext jspctx = getJspContext();
String linkStart = null;
String linkMid = null;
String linkEnd = null;
int pageNo = page==null?1:Integer.parseInt(page);
int pageTotal = Integer.parseInt(this.pageCount);
String jsp = "";
if(pageTotal != 1){
if(this.url.contains("?")){
linkStart="<a href='"+this.url+"&page=";
}else{
linkStart = "<a href='"+this.url+"?page=";
}
linkMid = "'>";
linkEnd = "</a>";
jsp = "<div id=\"pager_link\">";
if(pageNo != 1){
jsp +=linkStart+(pageNo-1)+linkMid+"上一页"+linkEnd;
}
for(int i=1;i<pageTotal+1;i++){
if(i==pageNo){
jsp+=i;
}else{
jsp += linkStart+i+linkMid+i+linkEnd;
}
}
if(pageNo != pageTotal){
jsp +=linkStart+(pageNo+1)+linkMid+"下一页"+linkEnd;
}
jsp += "</div>";
}
jspctx.setAttribute("page_link", jsp);
}
}
标签描述文件pager-tag.tld
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd"> <taglib> <tlibversion>1.0</tlibversion> <jspversion>1.1</jspversion> <shortname>lectery</shortname> <uri>http://lectery/pager-tag</uri> <tag> <name>pager</name> <tagclass>lectery.tag.PagerTag</tagclass> <bodycontent>empty</bodycontent> <attribute> <name>page</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>url</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>pageCount</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> <tag> <name>welcome</name> <tagclass>org.guava.tag.SimpleTag</tagclass> <bodycontent>empty</bodycontent> <attribute> <name>name</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>pageNo</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>pageCount</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib>
分页用的Bean
package lectery.util;
import java.util.List;
public class PageModel {
private int total;
private List list;
private int pageNo;
private int pageSize;
private int totalPage;
public int getTotal() {
return total;
}
public int getTotalPage() {
return (total + pageSize - 1) / pageSize;
}
public void setTotal(int totalRecords) {
this.total = totalRecords;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getPageNo() {
return pageNo;
}
public void setPageNo(int pageNo) {
this.pageNo = pageNo;
}
public int getTopPageNo() {
return 1;
}
public int getPreviousPageNo() {
if (pageNo <= 1) {
return 1;
}
return pageNo -1;
}
public int getNextPageNo() {
if (pageNo >= getTotalPage()) {
return getTotalPage()==0?1:getTotalPage();
}
return pageNo + 1;
}
public int getBottomPageNo() {
return getTotalPage() == 0?1:getTotalPage();
}
}
页面中的使用
<lectery:pager url="post/all" page="${page}" pageCount="${postsModel.totalPage}"/> ${page_link } </body>