Pager这个类是用来装载分页信息的.如当前是第几页,每一页有多少数据等等
package org.jksoft.pager;
public class Pager {
/**
* 用来控制分页信息.
* @author jackey
*
*/
private int currentPage;// 当前第几页
private int totalRows; // 一共有多少行数据
private int totalPages;// 总页数
private int pageSize = 10; // 一页有多少数据,默认为10
private boolean hasNext;// 是否有下一页
private boolean hasPrevious;// 是否有前一页
private boolean isLast;// 当前页 是否是最后一页
public Pager() {
this.currentPage = 1; // 初始化为第一页
}
public void init(int totalRows, int pageSize) {
if (pageSize != 0) { // 如果传过来的参数不为0才赋值,否则使用默认值
this.pageSize = pageSize;
}
if (totalRows != -1)//-1表示不传参数,只是调用init方法.
this.totalRows = totalRows;
this.hasPrevious = false;// 无上一页
if (this.totalRows % this.pageSize == 0) {
this.totalPages = this.totalRows / this.pageSize;
} else {
this.totalPages = this.totalRows / this.pageSize + 1;
}// 计算页数
refresh();
}
/**
* 根据参数判断是否有上一页,下一页,尾页等等.
* 刷新用.
* @author jackey
*/
public void refresh() {
if (currentPage == 1) {
this.hasPrevious = false;
}
if (currentPage > 1) {// 当前页大于1,有上一页,首页
this.hasPrevious = true;
}
if (this.totalPages > this.currentPage) {// 如果页数大于当前页 则有下一页 和尾页
this.hasNext = true;
this.isLast = false;
}
if (this.totalPages == this.currentPage) {// 如果总页数等于当前页,那么是最后一页
this.isLast = true;
this.hasNext = false;
}
if (this.totalPages < this.currentPage) {// 如果当前页
return;
}
}
//是否首页
public boolean isFirstPage(){
if(this.currentPage == 1){
return true;
}
return false;
}
//是否最后一页.
public boolean isLastPage(){
if(this.isLast){
return true;
}
return false;
}
//首页,当前页码 = 1
public void first(){
this.currentPage = 1;
refresh();
}
//下一页,当前页码 + 1;
public void next(){
this.currentPage += 1;
if(this.currentPage >= this.totalPages){
this.currentPage = this.totalPages;
}
refresh();
}
//上一页,当前页码 - 1
public void previous(){
this.currentPage -= 1;
if(this.currentPage <= 0){
this.currentPage = 1;
}
refresh();
}
//尾页,当前页码等于总页数
public void last(){
this.currentPage = this.totalPages;
refresh();
}
public int getCurrentPage() {
return currentPage;
}
//设置了当前页后要修改参数.
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
refresh();
}
//获取总行数 .
public int getTotalRows() {
return totalRows;
}
//修改总行数,要重新计算总页数,以及其他参数.
public void setTotalRows(int totalRows) {
this.init(totalRows, 0);
}
//获取总页数
public int getTotalPages() {
return totalPages;
}
//获得页大小
public int getPageSize() {
return pageSize;
}
//获得 "是否 有下一页"
public boolean isHasNext() {
return this.hasNext;
}
//获得是否有上一页
public boolean isHasPrevious() {
return this.hasPrevious;
}
//是否最后一页.
public boolean isIsLast() {
return this.isLast;
}
}
------------------------------------------------------------------
PagerHelp是用来从session中取Pager的.如果没有则new一个.
package org.jksoft.pager;
import javax.servlet.http.HttpServletRequest;
public class PagerHelp {
public static Pager getPager(HttpServletRequest request, int totalRows,
int pageSize) {
Pager pager = null;
Object obj= request.getSession().getAttribute("_pager");
if (obj == null) {
pager = new Pager();
pager.init(totalRows, pageSize);
}else
pager = (Pager)obj;
String page = "";//request.getParameter("page");
// page = page.trim().toLowerCase();
if (page == null || "".equals(page) || "first".equals(page)) {
pager.first();
} else if ("previous".equals(page)) {
pager.previous();
} else if ("next".equals(page)) {
pager.next();
} else if ("last".equals(page)) {
pager.last();
}
return pager;
}
}
PagerTag用来显示标签.
package org.jksoft.pager;
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class PagerTag extends BodyTagSupport {
String url;
String page;
Pager pager;
String forward;
int total;
int size;
public int doStartTag() throws JspException {
HttpServletRequest request = (HttpServletRequest) pageContext
.getRequest();
System.out.println(total + " --------- " + size);
// 取pager.保存分页信息的类.
pager = PagerHelp.getPager(request, total, size);
// out对象
JspWriter out = pageContext.getOut();
pager.setCurrentPage(6);
try {
out.println("<style type='text/css'>" + "<!--"
+ "a:link { text-decoration: none;color: blue}"
+ "a:active { text-decoration:blink}"
+ "a:hover { text-decoration:underline;color: red}"
+ "a:visited { text-decoration: none;color: green}" + "-->"
+ "</style>");
if (pager.isHasNext() && pager.isHasPrevious()) {
// 中间某页
System.out.println("if");
out.println("<a href='" + forward + "' page='first'>首页</a>");
out.println("<a href='" + forward + "' page='next'>下一页</a>");
out
.println("<a href='" + forward
+ "' page='previous'>上一页</a>");
out.println("<a href='" + forward + "' page='last'>尾页</a>");
} else if (pager.isFirstPage() && pager.isHasNext()) {
// 首页,有下一页
System.out.println("else if 1");
out.println("<a href='#' page='first'>首页</a>");
out.println("<a href='" + forward + "' page='next'>下一页</a>");
out.println("<a href='#' page='previous'>上一页</a>");
out.println("<a href='" + forward + "' page='last'>尾页</a>");
} else if (pager.isHasPrevious() && pager.isIsLast()) {
// 最后一页有上一页
System.out.println("else if 2");
out.println("<a href='" + forward + "' page='first'>首页</a>");
out.println("<a href='#' page='next'>下一页</a>");
out
.println("<a href='" + forward
+ "' page='previous'>上一页</a>");
out.println("<a href='#' page='last'>尾页</a>");
} else {
System.out.println("else");
out.println("<a href='#' page='first'>首页</a>");
out.println("<a href='#' page='next'>下一页</a>");
out.println("<a href='#' page='previous'>上一页</a>");
out.println("<a href='#' page='last'>尾页</a>");
}
out.println("<font color='green'> 共有 " + pager.getTotalRows()
+ " 条数据 当前第 "
+ pager.getCurrentPage() + "页 / 共 "
+ pager.getTotalPages() + " 页</font>");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
request.getSession().setAttribute("_pager", pager);
return EVAL_BODY_INCLUDE;
}
public int doAfterBody() throws JspException {
return SKIP_BODY;
}
public int doEndTag() throws JspException {
return SKIP_PAGE;
}
public void doInitBody() throws JspException {
super.doInitBody();
}
public void setBodyContent(BodyContent b) {
// TODO Auto-generated method stub
super.setBodyContent(b);
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
}
-------------------------------------------------------------------
编写tld文件,
<?xml version = "1.0" encoding = "utf-8" ?>
<taglib version="2.0"
xmlns = "http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:shcemalocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<description>自定义标签</description>
<display-name>JSTL core</display-name>
<tlib-version>1.1</tlib-version>
<short-name>firstLabel</short-name>
<uri>http://java.sun.com/jsp/jstl/core</uri>
<tag>
<name>LittlePager</name>
<tag-class>org.jksoft.pager.PagerTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>total</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>size</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
--------------------------------------------------------------
在页面里
<%@taglib uri="/WEB-INF/pager.tld" prefix = "pager" %>
<pager:LittlePager total="100" size="5"></pager:LittlePager>
引入标签并使用就可以了.
可能有许多地方不正确,欢迎指正