package org.whvcse.common;
import java.io.Serializable;
/**
* @desc 分页容器类
* @author mengdejun
*/
public class Page implements Serializable
{
private static final long serialVersionUID = 1L;
private int allPage = 0;//总页数
private int allRecords = 0;//总记录
private int currentPage = 1;//当前页
private String link = null;//链接信息
private int perRecords = 5;//显示条数
public int getAllPage() {
return allPage;
}
public int getAllRecords() {
return allRecords;
}
public int getCurrentPage() {
return currentPage;
}
/**
*
* @return 容器索引结束坐标
*/
public int getEndIndex() {
return ((this.currentPage - 1) * this.perRecords + this.perRecords > this.allRecords) ? this.allRecords
: (this.currentPage - 1) * this.perRecords + this.perRecords;
}
public String getLink() {
return link;
}
public int getPerRecords() {
return perRecords;
}
/**
*
* @return 容器索引开始坐标
*/
public int getStartIndex() {
return (this.currentPage - 1) * this.perRecords;
}
public boolean hasNext() {
return this.currentPage < this.allPage;
}
public boolean hasPrevious() {
return this.currentPage > 1;
}
public void setAllPage(int allPage) {
this.allPage = (allPage % this.perRecords == 0) ? (allPage / this.perRecords)
: (allPage / this.perRecords + 1);
}
public void setAllRecords(int allRecords) {
this.allRecords = allRecords;
}
public void setCurrentPage(int currentPage) {
if (currentPage < 1)
this.currentPage = 1;
else if (currentPage > this.allPage)
this.currentPage = this.allPage;
else
this.currentPage = currentPage;
}
public void setCurrentPage(String currentPage) {
if (null == currentPage || currentPage.equals(""))
currentPage = "1";
try {
this.setCurrentPage(Integer.parseInt(currentPage));
} catch (NumberFormatException e) {
this.currentPage = 1;
}
}
public void setLink(String link) {
this.link = link;
}
public void setPerRecords(int perRecords) {
this.perRecords = perRecords;
}
public void setPerRecords(String perRecords) {
try {
this.perRecords = Integer.parseInt(perRecords);
} catch (NumberFormatException e) {
this.perRecords = 5;
}
}
}
package org.whvcse.ext;
import java.io.Serializable;
import javax.servlet.ServletRequest;
import org.whvcse.common.Page;
/**
* @desc 标签生成器接口 拓展生成器需实现该接口并返回链接字符串
* @author mengdejun
*/
public interface LinkHandler extends Serializable
{
/**
*
* @param page 分页类信息
* @param request httpServletRequest对象封装
* @param format 链接信息格式化
* @param param 当前分页标签变量
* @return 链接字符串
* @throws NullPointerException
*/
public String parse(Page page,ServletRequest request,String format,String param) throws NullPointerException;
}
package org.whvcse.ext.link;
import javax.servlet.ServletRequest;
import org.whvcse.common.Page;
import org.whvcse.ext.LinkHandler;
/**
* @desc 默认链接生成器,该类需自己扩展以实现自己的样式,需实现LinkHandler接口,自定义时候请在标签cls属性填入类的全名
* @author mengdejun
*/
public class GeneralLinkHandler implements LinkHandler
{
private static final long serialVersionUID = 1L;
public String parse(Page page,ServletRequest request,String format,String param) throws NullPointerException
{
String returnStr="共"+page.getAllPage()+"页,第"+page.getCurrentPage()+"页 ";
for(int i=1;i<=page.getAllPage();i++)
{
returnStr+="<a href=index.jsp?"+param+"="+i+">"+i+"</a> ";
}
return returnStr;
}
}
package org.whvcse.page;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import org.whvcse.ext.LinkHandler;
public class TagLink extends TagSupport
{
private static final long serialVersionUID = 1L;
private TagPage page=null;//父容器
private String format="共{allPage},第{currentPage}";//链接格式,可自定义
private LinkHandler handler=null;//链接处理类
private String var=null;//返回信息
private String cls="org.whvcse.ext.link.GeneralLinkHandler";//默认链接类
@SuppressWarnings({ "static-access", "unchecked" })
@Override
public int doEndTag() throws JspException
{
//获取父容器标签对象
this.page=(TagPage) this.findAncestorWithClass(this,TagPage.class);
JspWriter out=this.pageContext.getOut();
try
{
Class c = Class.forName(this.cls);//插件设置,加载自定义插件类
this.handler=(LinkHandler) c.newInstance();
String temp=this.handler.parse(this.page.getPage(),pageContext.getRequest(),this.format,this.page.getParam());
if(null==this.var)
{
out.println(temp);
}
else
{
this.pageContext.setAttribute(this.var,temp,this.page.getScope(this.page.getScope()));
}
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return EVAL_PAGE;
}
public String getFormat()
{
return format;
}
public void setFormat(String format) {
this.format = format;
}
public String getCls() {
return cls;
}
public void setCls(String cls) {
this.cls = cls;
}
public String getVar() {
return var;
}
public void setVar(String var) {
this.var = var;
}
}
package org.whvcse.page;
import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import org.whvcse.common.Page;
public class TagPage extends BodyTagSupport
{
private static final long serialVersionUID = 1L;
private String id=null;//数据源ID
private String target=null;//目标源,处理后的链表
private String scope=null;//变量作用域
private String param=null;//当前页标签变量
@SuppressWarnings("unchecked")
private List list=null;
private Page page=null;//分页类
private String rows="5";//显示行数
public TagPage()
{
this.scope="request";
this.param="page";
this.target="pageData";
}
@Override
public int doEndTag() throws JspException
{
super.pageContext.removeAttribute(this.id, this.getScope(this.scope));
return EVAL_PAGE;
}
@SuppressWarnings("unchecked")
@Override
public int doStartTag() throws JspException
{
this.list=(List) super.pageContext.getAttribute(this.id,this.getScope(this.scope));
this.page=new Page();
this.page.setAllRecords(this.list.size());
this.page.setPerRecords(this.rows);
this.page.setAllPage(this.list.size());
String start=super.pageContext.getRequest().getParameter(this.param);
this.page.setCurrentPage(start);
this.list=this.list.subList(this.page.getStartIndex(),this.page.getEndIndex());
super.pageContext.setAttribute(this.target, this.list, this.getScope(this.scope));
return EVAL_BODY_INCLUDE;
}
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
public String getTarget()
{
return target;
}
public void setTarget(String target)
{
this.target = target;
}
public String getScope() {
return scope;
}
public void setScope(String scope) {
this.scope = scope;
}
public String getParam() {
return param;
}
public void setParam(String param) {
this.param = param;
}
@SuppressWarnings("static-access")
public int getScope(String scope)
{
if(scope.equals("request")||scope.equals("REQUEST")||scope.equals("r"))return super.pageContext.REQUEST_SCOPE;
if(scope.equals("session")||scope.equals("SESSION")||scope.equals("s"))return super.pageContext.SESSION_SCOPE;
if(scope.equals("application")||scope.equals("APPLICATION")||scope.equals("a"))return super.pageContext.APPLICATION_SCOPE;
if(scope.equals("page")||scope.equals("PAGE")||scope.equals("p"))return super.pageContext.PAGE_SCOPE;
return super.pageContext.REQUEST_SCOPE;
}
public String getRows()
{
return rows;
}
public void setRows(String rows)
{
this.rows = rows;
}
public Page getPage()
{
return page;
}
}
<?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.3</tlib-version> <jsp-version>1.2</jsp-version> <short-name>whvcse</short-name> <uri>http://mengdejun.iteye.com</uri> <tag> <name>page</name> <tag-class>org.whvcse.page.TagPage</tag-class> <body-content>JSP</body-content> <description/> <attribute> <name>scope</name> <rtexprvalue>true</rtexprvalue> <description> 分页类 </description> </attribute> <attribute> <name>param</name> <required>false</required> <rtexprvalue>true</rtexprvalue> <description> 页码 </description> </attribute> <attribute> <name>rows</name> <required>false</required> <rtexprvalue>true</rtexprvalue> <description> 显示行数 </description> </attribute> <attribute> <name>id</name> <required>true</required> <rtexprvalue>true</rtexprvalue> <description> 变量列表 </description> </attribute> <attribute> <name>target</name> <required>true</required> <rtexprvalue>true</rtexprvalue> <description> 别名 </description> </attribute> </tag> <tag> <name>pageLink</name> <tag-class>org.whvcse.page.TagLink</tag-class> <body-content>empty</body-content> <description/> <attribute> <name>format</name> <required>false</required> <rtexprvalue>true</rtexprvalue> <description> 链接信息 </description> </attribute> <attribute> <name>cls</name> <required>false</required> <rtexprvalue>true</rtexprvalue> <description> 扩展信息,实现LinkHandler接口 </description> </attribute> <attribute> <name>var</name> <required>false</required> <rtexprvalue>true</rtexprvalue> <description> 该属性将不输出分页信息,将信息赋给var变量 </description> </attribute> </tag> </taglib>
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib prefix="whvcse" uri="http://mengdejun.iteye.com"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>分页标签实例</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<%
List a=new ArrayList();
for(int i=1;i<=50;i++)
{
a.add(i);
request.setAttribute("list",a);
}
%>
<body>
<whvcse:page id="list" target="olist" scope='request' rows='10'><!-- 分类标签 -->
<%
List o=(List)request.getAttribute("olist");
for(int i=0;i<o.size();i++)
{
out.print(o.get(i)+"<br/>");
}
%>
<whvcse:pageLink/>
</whvcse:page>
</body>
</html>