shh_mao 发表于 2013-2-6 08:07:38

自定义标签的分页

tld 文件:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>self</short-name>
<uri>http://bonc.com/web-page</uri>
<display-name>SELF core</display-name>
<description>Self 1.0 core library</description>


<tag>
    <name>pageNavigate</name>
    <tag-class>com.bean.tag.WebPageTag</tag-class>
    <body-content>empty</body-content>
    <description> </description>
    <attribute>
      <name>allRows</name>
      <required>false</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
       <attribute>
      <name>allPages</name>
      <required>false</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
       <attribute>
      <name>currentPage</name>
      <required>false</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
      <name>jumpPage</name>
      <required>false</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
</tag>

</taglib>

tag处理文件:
package com.bean.tag;

import java.io.IOException;

import javax.servlet.ServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

public class WebPageTag extends TagSupport {

private String size;
private String pageNum;
private String allPages;
private String jumpPage;


public int doEndTag() throws JspException {
return super.EVAL_BODY_INCLUDE;
}


public int doStartTag() throws JspException {
ServletRequest request=this.pageContext.getRequest();
    String pagesAll=request.getAttribute("allPages")+"";
    String numPage=request.getAttribute("pageNum")+"";
    String sizePage=request.getAttribute("pageSize")+"";
    String requestURI=request.getAttribute("requestURI")+"";
    String currentpage=request.getAttribute("currentPage")+"";
    String allRows =request.getAttribute("allrows")+"";
   
    String allrowsStr="共<font color='red'>"+allRows+"</font>条记录";
      String allpagesStr=" 共<font color='red'>"+pagesAll+"</font>页 ";
      String pageNumStr=" 当前是第<font color='red' >"+numPage+"</font>页 ";
      String pageSize=" 每页应有<font color='red'>"+sizePage+"</font>条记录 ";
      
      String selOpt = "";
      if(pagesAll==null)pagesAll="1";
      for(int i=1;i<=Integer.parseInt(pagesAll);i++)
{
      if(i==Integer.parseInt(numPage))
      {
      selOpt+= " <option value=\""+i+"\" selected>"+i+"</option> ";
      }else
      {
      selOpt+= " <option value=\""+i+"\">"+i+"</option> ";
      }
}
      String jumpPageStr = "跳入第 <select name=\"num\" onChange=\"jumppagego()\">" +selOpt+" </select> 页";
      
      String pageDesc = "共"+allRows+"项 第"+numPage+"/"+pagesAll+"页 ";
      
      String pagehome =" <a href=\"javascript:step_page('pagehome')\" >首页</a> ";
      String pageup   =" <a href=\"javascript:step_page('pageup')\">上一页</a> ";
      String pagedown =" <a href=\"javascript:step_page('pagedown')\">下一页</a> ";
      String pageend=" <a href=\"javascript:step_page('pageend')\">未页</a> ";
      
    if(allPages!=null&&allPages.equals("false")) {
          allpagesStr="";
   }
    if(pageNum!=null&&pageNum.equals("false")) {
          pageNumStr="";
    }
    if(size!=null&&size.equals("false")){
      pageSize="";
    }
    if(currentpage.equals("0")){
      pagehome=" <font color=#999999>首页</font> ";
      pageup=" <font color=#999999>上一页</font> ";
    }
    if(pagesAll.equals(numPage)){
      pageend =" <font color=#999999>未页</font> ";
      pagedown=" <font color=#999999>下一页</font> ";
    }
    if(pagesAll.equals("1")||pagesAll.equals("0")){
      pagehome=" <font color=#999999>首页</font> ";
         pageup=" <font color=#999999>上一页</font> ";
         pagedown=" <font color=#999999>下一页</font> ";
         pageend =" <font color=#999999>未页</font> ";
    }
         
         
    try {
pageContext.getOut().write(pageDesc
                           +pagehome+pageup+pagedown+pageend+jumpPageStr
               
               +"<script type=\"text/javascript\">"
               +"   function step_page(type){ "
               +"   address_listForm.action=\""+requestURI+"?type=\"+type+\"&currentpage="+currentpage+"&allrows="+allRows+"\";"
               +"   address_listForm.submit();}"
               +"function jumppagego()"
               +"{"
               +"var type = \"jump\";"
               +"var currentpage=address_listForm.num.value;"
               +"var url= \"address_list.do?type=\"+type+\"&currentpage=\"+currentpage+\"&allrows="+allRows+"\";"
               +"address_listForm.action=url;"
               +"address_listForm.submit();"
               +"}"
               +"</script>");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return EVAL_PAGE;   
}



public String getSize() {
return size;
}


public void setSize(String size) {
this.size = size;
}


public String getPageNum() {
return pageNum;
}


public void setPageNum(String pageNum) {
this.pageNum = pageNum;
}


public String getAllPages() {
return allPages;
}


public void setAllPages(String allPages) {
this.allPages = allPages;
}


public String getJumpPage() {
return jumpPage;
}


public void setJumpPage(String jumpPage) {
this.jumpPage = jumpPage;
}


}



类处理文件:
package com.bean.struts.util;

public class WebPages {

private int currentPage=0;//当前页从0开始编号
    private int allpages;
    private int allrows;
    private int pageSize=10;
    private int pageNum;

   private final static WebPages webPages=new WebPages();
   
   private WebPages() {}
public static WebPages getWebPages() {
return webPages;
}
public int getPageNum() {
return pageNum;
}

public void setPageNum(int pageNum) {
this.pageNum = pageNum;
}
   
public void setWebPages(javax.servlet.http.HttpServletRequest request)
{


String type=request.getParameter("type");
if(request.getParameter("currentpage")!=null && ! request.getParameter("currentpage").equals(""))
{
currentPage=Integer.parseInt(request.getParameter("currentpage"));
}else
{
currentPage=0;
}
if(type!=null && !type.equals(""))
{
if(type.equals("pageup")) {
currentPage-=1;
}else if(type.equals("pagedown"))
{
currentPage+=1;
}else if(type.equals("pagehome"))
{
currentPage=0;
}else if(type.equals("pageend"))
{

if(this.getAllrows()%this.getPageSize()!=0) {
currentPage=this.getAllrows()/this.getPageSize();
this.setAllpages(currentPage+1);
}else
{
currentPage=this.getAllrows()/this.getPageSize()-1;
this.setAllpages(currentPage);
}
}else if(type.equals("jump"))
{
currentPage-=1;
}

if(currentPage<0) {
currentPage=0;
}


}

this.setCurrentPage(currentPage);
this.setPageNum(this.getCurrentPage()+1);

request.setAttribute("currentPage",currentPage);
request.setAttribute("pageSize",this.getPageSize());


}
public void setWebPages(javax.servlet.http.HttpServletRequest request,int allrows)
{
this.setAllrows(allrows);
      this.setAllPages();

String type=request.getParameter("type");

if(request.getParameter("currentpage")!=null && ! request.getParameter("currentpage").equals(""))
{
currentPage=Integer.parseInt(request.getParameter("currentpage"));
}
if(type!=null && !type.equals(""))
{
if(type.equals("pageup")) {
currentPage-=1;
}else if(type.equals("pagedown"))
{
currentPage+=1;
}else if(type.equals("pagehome"))
{
currentPage=0;
}else if(type.equals("pageend"))
{

if(this.getAllrows()%this.getPageSize()!=0) {
currentPage=this.getAllrows()/this.getPageSize();
this.setAllpages(currentPage+1);
}else
{
currentPage=this.getAllrows()/this.getPageSize();
this.setAllpages(currentPage);
}
}else if(type.equals("jump"))
{
currentPage-=1;
}

if(currentPage<0) {
currentPage=0;
}
if(currentPage>=this.getAllpages()) {
currentPage=this.getAllpages()-1;
}

}

this.setCurrentPage(currentPage);
this.setPageNum(this.getCurrentPage()+1);

request.setAttribute("allPages",this.getAllpages());
request.setAttribute("pageNum",this.getPageNum());
request.setAttribute("currentPage",currentPage);
request.setAttribute("pageSize",this.getPageSize());
request.setAttribute("allrows", this.getAllrows());
String url=request.getRequestURI();
      String subUrl=url.substring(url.lastIndexOf("/")+1);
      request.setAttribute("requestURI",subUrl);
}
    public void setAllPages()
    {


if(this.getAllrows()%this.getPageSize()!=0) {
this.setAllpages(this.getAllrows()/this.getPageSize()+1);
}else
{
this.setAllpages(this.getAllrows()/this.getPageSize());
}
    }
public int getCurrentPage() {
return currentPage;
}

public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}

public int getAllpages() {
return allpages;
}

public void setAllpages(int allpages) {
this.allpages = allpages;
}

public int getPageSize() {
return pageSize;
}

public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}

public int getAllrows() {
return allrows;
}

public void setAllrows(int allrows) {
this.allrows = allrows;
}





}
页: [1]
查看完整版本: 自定义标签的分页