前几天,项目需要..写了一个分页组件..现在贴出来和大家一起分亨:
以下是标签类:
package com.zte.matermanage.bean;
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
/**
* 自定义标签分页类
* @author lizengsheng
* @author xiaoyong
*/
public class PagerTag extends TagSupport
{
/**
*
*/
private static final long serialVersionUID = 1L;
private int pageSize = 5; // 每页要显示的记录数
private int pageNo = 1; // 页号
private int recordCount; // 总记录数
private String url; // 目的地URL
public void setPageSize(int pageSize)
{
this.pageSize = pageSize;
}
public void setPageNo(int pageNo)
{
this.pageNo = pageNo;
}
public void setRecordCount(int recordCount)
{
this.recordCount = recordCount;
}
public void setUrl(String url)
{
this.url = url;
}
// 主要的逻辑
@Override
public int doStartTag() throws JspException
{
if (recordCount == 0)
{
return super.doStartTag();
}
// 总页数
int pageCount = (recordCount + pageSize - 1) / pageSize;
// 页号越界处理
if (pageNo > pageCount)
{
pageNo = pageCount;
}
if (pageNo < 1)
{
pageNo = 1;
}
StringBuilder sb = new StringBuilder();
sb
.append(
"<form name='pageController' id='pageController' action='' method='post'>/r/n")
.append(
"<input type='hidden' id='pageNo' name='pageNo' value='"
+ pageNo + "' />/r/n");
// 获取所有請求中的参数
HttpServletRequest request = (HttpServletRequest) pageContext
.getRequest();
Enumeration<String> enumeration = request.getParameterNames();
String name = null;
String value = null;
// 把请求中的所有参数当作隐藏表单域在页面中写出)
while (enumeration.hasMoreElements())
{
name = enumeration.nextElement();
value = request.getParameter(name);
// 去除页号
if (name.equals("pageNo"))
{
if (null != value && !"".equals(value))
{
pageNo = Integer.parseInt(value);
}
continue;
}
if(!name.equals("pageNumber"))
{
sb.append("<input type='hidden' name='").append(name).append(
"' value='").append(value).append("'/>/r/n");
}
}
sb.append("总共有" + pageCount + "页,").append(" 共有" + recordCount +"条记录, ")
.append("当前是第" + pageNo + "页 /r/n");
if (pageNo == 1)
{
sb.append("首页");
sb.append(" ");
sb.append("上一页/r/n");
}
else
{
sb.append("<a href='#' οnclick='turnOverPage(1)'>首页</a>/r/n");
sb.append(" ");
sb.append("<a href='#' οnclick='turnOverPage(")
.append((pageNo - 1)).append(")'>上一页</a>/r/n");
}
sb.append(" ");
if (pageNo == pageCount)
{
sb.append("下一页");
sb.append(" ");
sb.append("尾页/r/n");
}
else
{
sb.append("<a href='#' οnclick='turnOverPage(")
.append((pageNo + 1)).append(")'>下一页</a>/r/n");
sb.append(" ");
sb.append("<a href='#' οnclick='turnOverPage(").append(pageCount)
.append(")'>尾页</a>/r/n");
}
sb.append(" 转到第<input type='text' name='pageNumber' οnkeydοwn='onlyNum();' /> ")
.append("页")
.append("<input type='button' οnclick='turnOverPage(document.all.pageNumber.value)' value='GO' ");
sb.append("</select>/r/n");
sb.append("</form>/r/n");
// 生成提交表单的JS
sb.append("<script language='javascript'>/r/n");
sb.append(" //翻页函数/t/n");
sb.append(" function turnOverPage(no){/r/n");
sb.append(" var form = document.pageController;/r/n");
sb.append(" //页号越界处理/r/n");
sb.append(" if(no").append(">").append(pageCount).append(") {/r/n");
sb.append(" no=").append(pageCount).append(";/r/n");
sb.append(" }/r/n");
sb.append(" else if(no").append("< 1){/r/n");
sb.append(" no=1;/r/n");
sb.append(" }/r/n");
sb.append(" form.").append("pageNo").append(".value=no;/r/n");
sb.append(" form.action='").append(url).append("';/r/n");
sb.append(" form.submit();/r/n");
sb.append(" }/r/n");
sb.append(" function onlyNum()/n") ;
sb.append(" {/nif(!((event.keyCode>=48&&event.keyCode<=57)");
sb.append(" ||(event.keyCode==8)") ;
sb.append(" ||(event.keyCode>=96&&event.keyCode<=105)))/n") ;
sb.append(" event.returnValue=false;}/n");
sb.append("</script>/r/n");
try
{
pageContext.getOut().println("/t" + sb.toString());
}
catch (IOException e)
{
e.printStackTrace();
}
return super.doStartTag();
}
}
以下后台截取页面显示的数据,设置在REQUEST 作用域中:
package com.zte.matermanage.common;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
public class PageUtil
{
/**
* 每页显示条数
*/
private static int pageSize = 5;
/**
* 分页公共方法
* @param request
* @param list
*/
public static void pageComm(HttpServletRequest request, List list,
String url)
{
String pageNoStr = request.getParameter("pageNo");
int pageNo = 1;
if (pageNoStr != null && !pageNoStr.equals(""))
{
pageNo = Integer.parseInt(pageNoStr);
}
int recordCount = list.size();
int startNo = (pageNo - 1) * pageSize;
List result = new ArrayList();
// 提取数据
if (pageNo == (recordCount / pageSize + 1))
{
result = list.subList(startNo, recordCount);
}
else
{
result = list.subList(startNo, startNo + pageSize);
}
request.setAttribute("recordCount", recordCount);
request.setAttribute("pageNo", pageNo+"");
request.setAttribute("pageSize", pageSize);
request.setAttribute("data", result);
request.setAttribute("url", url);
}
}
//以下是设置标签的属性
<?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"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd">
<tlib-version>2.0</tlib-version>
<short-name>my</short-name>
<uri>/my</uri>
<tag>
<description>通用分页标签</description>
<name>pager</name>
<tag-class>com.zte.matermanage.bean.PagerTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>pageNo</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>recordCount</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>pageSize</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>url</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>