发现在系统中有好多写得很好的代码,也不知道是哪位前辈留下来的.整理一下...
自定义分页标签库
1:在配置文件中配置
<!-- self Tag Library Descriptors -->
<taglib>
<taglib-uri>/WEB-INF/self-html.tld</taglib-uri>
<taglib-location>/WEB-INF/self-html.tld</taglib-location>
</taglib> 2:在self-html.tld中page标签的配置
<tag>
<name>page</name>
<tagclass>com.jbbis.netedu.common.util.taglib.PageTag</tagclass>
<bodycontent>empty</bodycontent>
<info>tag here</info>
<attribute>
<name>name</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>pageNum</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>method</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>methodValue</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>form</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>action</name>
<required>flase</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>returnLevel</name>
<required>flase</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag> 其实从这儿也可以看到在PageTag文件中是一些什么值了...
3:PageTag中的具体操作(精华全部在这儿啦)
/**//*
* Created on 2004-6-25
*
*/
package com.jbbis.netedu.common.util.taglib;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import com.jbbis.netedu.common.page.NullPage;
import com.jbbis.netedu.common.page.Page;
/** *//**
* @author leo_deng
*
*/
public class PageTag extends TagSupport {
/**//* (non-Javadoc)
* @see javax.servlet.jsp.tagext.Tag#doStartTag()
*/
private String name;
private String pageNum="pageNum";
private String method="method";
private String methodValue;
private String form="mainForm";
private String action;
private String returnLevel;
private final int CONST_FIRST=1;
private final int CONST_NEXT=2;
private final int CONST_PRIOR=3;
private final int CONST_LAST=4;
public int doStartTag() throws JspException {
// TODO Auto-generated method stub
try{
JspWriter out = pageContext.getOut() ;
Page page=(Page)pageContext.getRequest().getAttribute(name);
if(page==null)
page=NullPage.instance();
out.print(getHtml(page.getPageNumber(),page.getPageCount()));
out.print(getJs(page.getPageNumber(),page.getPageCount()));
//jspwriter是一个隐含对象,用于向jsp网页输出内容。输出的目标由
// pagecontext.getout提供
//向jsp网页上下文输出
}catch(IOException ioException){
throw new JspException(ioException.getMessage() );
}//catch
return(SKIP_BODY);
}
/**//*
* <
* <!--
* function page_next(){
* form.action=""
* form.pageNum=""
* form.method.value=""
* form.submit;
*
* }
* function page_prior()
* function page_first()
* funciotn page_last()
*
* //-->
* <div class="listbox_page">
<a class="page" ><img src="public/icon/first.gif" alt="首页" align="absmiddle" border="0" onclick="page_first()"/></a>
<a class="page" ><img src="public/icon/previous.gif" alt="上页" align="absmiddle" border="0" onclick="page_first()"/></a>
第<input class="page_number" name="pageNum" value=""> <img src="page_go.gif" class="page_go">页
<a class="page" ><img src="public/icon/next.gif" alt="下页" align="absmiddle" border="0"/ onclick="page_next()"></a>
<a class="page" ><img src="public/icon/last.gif" alt="末页" align="absmiddle" border="0"/ onclick="page_prior()"></a>
共 <span class="page_sum">1000</span> 页
</div>
*
* */
private String getJs(int pageNum,int totalNum){
StringBuffer sb=new StringBuffer();
sb.append("\n<script language=\"JavaScript\">\n<!--");
sb.append(printFunction("page_first",pageNum,totalNum,CONST_FIRST));
sb.append(printFunction("page_next",pageNum,totalNum,CONST_NEXT));
sb.append(printFunction("page_prior",pageNum,totalNum,CONST_PRIOR));
sb.append(printFunction("page_last",pageNum,totalNum,CONST_LAST));
getGoJs(sb,pageNum,totalNum);
sb.append("\n //-->\n</script>");
return sb.toString();
}
private void getGoJs(StringBuffer sb,int pageNum,int totalNum){
sb.append("function page_go(obj){\n");
sb.append("var page_number=document.getElementById(\"page_number\");\n");
sb.append("if( page_number.value != new Number(page_number.value))return alert(\"请输入正确的页数\");\n");
sb.append("if(page_number.value<1) page_number.value=1;if(page_number.value>");
sb.append(totalNum);
sb.append(")page_number.value=");
sb.append(totalNum);
sb.append(";\n");
sb.append("if(page_number.value!="+pageNum+")");
sb.append("submitForPageNum(obj);\n");
sb.append("}");
}
private StringBuffer printFunction(String function,int pageNumber,int totalNumber,int pageConst){
StringBuffer sb=new StringBuffer();
sb.append("\nfunction "+function+"(){");
sb.append("\nevent.returnValue=false;");
sb.append(printAction());
sb.append(printMethod());
//sb.append("\n"+this.form+".submit();\n}");
if(totalNumber>1){
switch(pageConst){
case CONST_FIRST:
if(pageNumber>1){
sb.append(printPageNum(String.valueOf(1)));
sb.append("\n submitForPageNum("+this.form+");");
}
break;
case CONST_LAST:
if(pageNumber<totalNumber){
sb.append(printPageNum(String.valueOf(totalNumber)));
sb.append("\n submitForPageNum("+this.form+");");
}
break;
case CONST_PRIOR:
if(pageNumber>1){
sb.append(printPageNum(String.valueOf(pageNumber-1)));
sb.append("\n submitForPageNum("+this.form+");");
}
break;
case CONST_NEXT:
if(pageNumber<totalNumber){
sb.append(printPageNum(String.valueOf(pageNumber+1)));
sb.append("\n submitForPageNum("+this.form+");");
}
break;
}
}
sb.append("\n}");
return sb;
}
/**//*
private StringBuffer printFunction(String function,String pageNum,int pageConst){
StringBuffer sb=new StringBuffer();
sb.append("\nfunction "+function+"(){");
sb.append("\nevent.returnValue=false;");
sb.append(printAction());
sb.append(printMethod());
sb.append(printPageNum(pageNum));
//sb.append("\n"+this.form+".submit();\n}");
sb.append("\n submitForPageNum("+this.form+");\n}");
sb.append("\n}");
return sb;
}
*/
private String printAction(){
if(this.action!=null)
return ("\n"+this.form+".action=\""+action+"\";");
else
return "";
}
private String printMethod(){
if(this.methodValue!=null)
return ("\n"+this.form+"."+this.method+".value=\""+this.methodValue+"\";");
else
return "";
}
private String printPageNum(String num){
return "\n"+this.form+"."+this.pageNum+".value=\""+num+"\";";
}
private String getHtml(int pageNum,int totalNum){
StringBuffer sb=new StringBuffer();
sb.append("\n<a class=\"page\" href=\"javascript:void(0)\" onclick=\"page_first()\" ><img src=\"public/icon/first.gif\" alt=\"首页\" align=\"absmiddle\" border=\"0\" /></a>");
sb.append("\n<a class=\"page\" href='javascript:void(0)' onclick='page_prior()' ><img src=\"public/icon/previous.gif\" alt=\"上页\" align=\"absmiddle\" border=\"0\" /></a>");
sb.append("\n第<input id=\"page_number\" class=\"page_number\" name=\"pageNum\" value=\"");
sb.append(String.valueOf(pageNum));
sb.append("\"><button class='page_go' onclick='page_go(this.form);'></button>页 ");
//sb.append(""<img src=\"page_go.gif\" class=\"page_go\">");
sb.append("\n<a class=\"page\" href=\"javascript:void(0)\" onclick=\"page_next()\"><img src=\"public/icon/next_page.gif\" alt=\"下页\" align=\"absmiddle\" border=\"0\"/></a>");
sb.append("\n<a class=\"page\" href=\"javascript:void(0)\" onclick=\"page_last()\"><img src=\"public/icon/last.gif\" alt=\"末页\" align=\"absmiddle\" border=\"0\"/></a>");
sb.append("\n共 <span class=\"page_sum\">");
sb.append(String.valueOf(totalNum));
sb.append("</span> 页");
return sb.toString();
}
/** *//**
* @return Returns the pageNum.
*/
public String getPageNum() {
return pageNum;
}
... ....(other getter and setter method ^_^)
}
4:在JSP布面中导入:
<%@ taglib uri="/WEB-INF/self-html.tld" prefix="netedu" %> 5:在JSP页面中引用:
<form name="newsForm" method="post" action="<%=request.getContextPath() %>/portal/newsAction.page" id="newsForm">
<!-- TIP008:
翻页部分
-->
<div class="listbox_page">
<netedu:page name="page" form="newsForm" />
</div>
<!-- TIP008:END-->
</form> 6:page文件
/**//*
* Created on 2004-6-8
*/
package com.jbbis.netedu.common.page;
import java.util.Collection;
/** *//**
* 分页表示 Bean
* 用于前台显示分页数据
* @author qqs
*/
public interface Page {
/** *//**
* whether or not is the last page of list data
* 是否为最后一页
* @return
*/
public boolean isLast();
/** *//**
* 是否为第一页
* @return
*/
public boolean isFirst();
/** *//**
* List of page data
* @return Page data
*/
public Collection getData();
/** *//**
* List of page data
* @return Page data
*/
public void setData(Collection data);
/** *//**
* 当前页数
* @return Page Number
*/
public int getPageNumber();
/** *//**
* Next page number
* if current page is the first page then
* return 0;
* else
* return pageNumber+1;
* 下一页的页数
*
* @return next page number
*/
public int getNextPageNumber();
/** *//**
* 前一页的页数
* if previous pge lest than last the first page then
* return 0
* esle
* return PageNumber - 1
* @return previous page number
*/
public int getPrevPageNumber();
/** *//**
* Current page size
* 当前页的记录数
* @return Current page size
*/
public int getSize();
/** *//**
* Page size
* 定义的每页的页数
* @return
*/
public int getPageSize();
/** *//**
* Total pages count
* 总页数
* @return Total pages count
*/
public int getPageCount();
/** *//**
* 当前页中第一条记录在数据库中的顺序
* get index of the first data of the page
* @return index of the first data of the page
*/
public int getStartIndex();
/** *//**
* get index of the last data of the page
* 当页的最后一条记录在数据库中的顺序
* @return index of the first data of the page
*/
public int getEndIndex();
/** *//**
* 总的记录数
* @return
*/
public int getTotalSize();
/** *//**
* 是否为空页
* @return
*/
public boolean isNull();
}7:其它
7.1
可以注意到在标签的定义文件中用到了名为page_go的css层来美化界面,可能是出于满足不同用户对界面的不同要求所以在此没有对此css定义,你可以在JSP布面中重写此css.这儿有个可以参考一下.
下载7.2
根据标签的定义可以看到,此page应该放到form中.
7.3
根据在标签库中的
Page page=(Page)pageContext.getRequest().getAttribute(name); 语句,可见page应该在request.setAttribute("pageattribute",page);中定义.
7.4
如何通过对DB的操作得到了page ?通过DB操作可以获得一系统的data(应该注意到了在page文件夹中有个data属性,高人就是高人^_^),当然在得到这些data之前,先可能得进行一系统逻辑处理,比如说当前页码(pagenum),页长(pagesize)等等.通过这些数据就可以得到一个page实例了! ~ ~ : )
... ...
相关:一个相当好的自定义标签---分页(二)