jsp中用自定义标签实现简单的分页

昨天学了一天的自定义标签,大概了解了其原理,就写了一个实现简单分页的程序。

1.它是基于我自己写的分页类(Pager)的,其代码如下:

package com.neusoft.training.database.oe;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionForm;



/**
 * <p>Description: 获得一个page,用于分页。</p>
 * @author  <a href="mailto: chengtj@neusoft.com">程天俊</a>
 * @version $Revision: 1.3 $
 */
public class Pager
{
    /**
     * <p>Discription:查询的总行数</p>
     */
    private int totalRows;

    /**
     * <p>Discription:每页显示的记录数</p>
     */
    private int pageSize = 10;

    /**
     * <p>Discription:现在所在的页数</p>
     */
    private int currentPage;

    /**
     * <p>Discription:总页数</p>
     */
    private int totalPages;

    /**
     * <p>Discription:本页的开始行</p>
     */
    private int startRow;
   
    /**
     * <p>Discription:存放查询条件,一般是ActionForm类型的form</p>
     */
    private ActionForm form;

    /**
     * <p>Discription:构造一个无参的pager对象</p>
     * @coustructor 方法.
     */
    public Pager()
    {
        this.currentPage = 1;
        this.startRow = 0;
    }
   
    /**
     * <p>Discription:根据request(pagesize和pagemethod)和totalrows设置pager对象的属性</p>
     * @coustructor 方法.
     */
    public void checkRequest(HttpServletRequest httpServletRequest)
    {
        String pageSizep = httpServletRequest.getParameter("pageSize");
        if (pageSizep != null)
        {
            this.pageSize=Integer.parseInt(pageSizep);
        }
       
       
       
        this.totalPages = this.totalRows / this.pageSize;
        int mod = this.totalRows % this.pageSize;
        if (mod > 0)
        {
            this.totalPages++;
        }
       
       
        String currentPage = httpServletRequest.getParameter("currentPage");
        if (currentPage != null)
        {
            this.refresh(Integer.parseInt(currentPage));  
        }
       
        String pagerMethod = httpServletRequest.getParameter("pageMethod");

        if (pagerMethod != null)
        {
            if (pagerMethod.equals("first"))
            {
                this.first();
            }
            else if (pagerMethod.equals("previous"))
            {
                this.previous();
            }
            else if (pagerMethod.equals("next"))
            {
                this.next();
            }
            else if (pagerMethod.equals("last"))
            {
                this.last();
            }
        }
    }
  
    /**
     * <p>Discription:获得pager的开始行</p>
     * @return
     * @author:程天俊
     * @update:[日期YYYY-MM-DD] [更改人姓名][变更描述]
     */
    public int getStartRow()
    {
        return startRow;
    }

    /**
     * <p>Discription:获得pager的总行数</p>
     * @return
     * @author:程天俊
     * @update:[日期YYYY-MM-DD] [更改人姓名][变更描述]
     */
    public int getTotalPages()
    {
        return totalPages;
    }


    /**
     * <p>Discription:获得pager的当前页</p>
     * @return
     * @author:程天俊
     * @update:[日期YYYY-MM-DD] [更改人姓名][变更描述]
     */
    public int getCurrentPage()
    {
        return currentPage;
    }

    /**
     * <p>Discription:获得pager的每页显示的记录数</p>
     * @return
     * @author:程天俊
     * @update:[日期YYYY-MM-DD] [更改人姓名][变更描述]
     */
    public int getPageSize()
    {
        return pageSize;
    }

    /**
     * <p>Discription:设置pager的总记录数</p>
     * @return
     * @author:程天俊
     * @update:[日期YYYY-MM-DD] [更改人姓名][变更描述]
     */
    public void setTotalRows(int totalRows)
    {
        this.totalRows = totalRows;
    }

    /**
     * <p>Discription:设置pager的开始行</p>
     * @return
     * @author:程天俊
     * @update:[日期YYYY-MM-DD] [更改人姓名][变更描述]
     */
    public void setStartRow(int startRow)
    {
        this.startRow = startRow;
    }

    /**
     * <p>Discription:设置pager的总页数</p>
     * @return
     * @author:程天俊
     * @update:[日期YYYY-MM-DD] [更改人姓名][变更描述]
     */
    public void setTotalPages(int totalPages)
    {
        this.totalPages = totalPages;
    }

    /**
     * <p>Discription:设置pager的当前页</p>
     * @return
     * @author:程天俊
     * @update:[日期YYYY-MM-DD] [更改人姓名][变更描述]
     */
    public void setCurrentPage(int currentPage)
    {
        this.currentPage = currentPage;
    }

    /**
     * <p>Discription:设置pager的每页显示的记录数</p>
     * @return
     * @author:程天俊
     * @update:[日期YYYY-MM-DD] [更改人姓名][变更描述]
     */
    public void setPageSize(int pageSize)
    {
        this.pageSize = pageSize;
    }

    /**
     * <p>Discription:设置pager的总记录数</p>
     * @return
     * @author:程天俊
     * @update:[日期YYYY-MM-DD] [更改人姓名][变更描述]
     */
    public int getTotalRows()
    {
        return totalRows;
    }

    /**
     * <p>Discription:设置pager的首页执行的操作</p>
     * @return
     * @author:程天俊
     * @update:[日期YYYY-MM-DD] [更改人姓名][变更描述]
     */
    public void first()
    {
        currentPage = 1;
        startRow = 0;
    }

    /**
     * <p>Discription:设置pager的上一页执行的操作</p>
     * @return
     * @author:程天俊
     * @update:[日期YYYY-MM-DD] [更改人姓名][变更描述]
     */
    public void previous()
    {
        if (currentPage == 1)
        {
            return;
        }
        currentPage--;
        startRow = (currentPage - 1) * pageSize;
    }

    /**
     * <p>Discription:设置pager的下一页执行的操作</p>
     * @return
     * @author:程天俊
     * @update:[日期YYYY-MM-DD] [更改人姓名][变更描述]
     */
    public void next()
    {
        if (currentPage < totalPages)
        {
            currentPage++;
        }
        startRow = (currentPage - 1) * pageSize;
    }

    /**
     * <p>Discription:设置pager的末页执行的操作</p>
     * @return
     * @author:程天俊
     * @update:[日期YYYY-MM-DD] [更改人姓名][变更描述]
     */
    public void last()
    {
        currentPage = totalPages;
        startRow = (currentPage - 1) * pageSize;
    }

    /**
     * <p>Discription:刷新pager的当前页</p>
     * @return
     * @author:程天俊
     * @update:[日期YYYY-MM-DD] [更改人姓名][变更描述]
     */
    public void refresh(int _currentPage)
    {
        currentPage = _currentPage;
        if (currentPage > totalPages)
        {
            last();
        }
    }

    public ActionForm getForm()
    {
        return form;
    }

    public void setForm(ActionForm form)
    {
        this.form = form;
    }
}
2.获得一个pager的方法,其用法用下:

public Pager getComPager(HttpServletRequest request,
            CommodityTypeForm commodityTypeForm) throws SQLException,
            IllegalAccessException, InvocationTargetException
    {
        Connection conn = DBPool.getPool().getConnection();
        CommodityTypeDao commodityTypeDao = new CommodityTypeDao();
        commodityTypeDao.setConn(conn);
       
        CommodityType commodityType = new CommodityType();
        BeanUtils.copyProperties(commodityType, commodityTypeForm);
        int totalRows = commodityTypeDao.getRows(commodityType);


        //创建一个Pager对象,并设置其属性
        Pager pager = new Pager();
        pager.setForm(commodityTypeForm);
        pager.setTotalRows(totalRows);
        pager.checkRequest(request);


        if (conn != null)
        {
            conn.close();
        }
        return pager;
    }

3.根据获得的Pager对象去查询记录(主要是在action中用其查询条件form,pagesize和startrow):

if (comTypeId == null)

//如果request中没有form对象来判断是不是分页查询,是就从session中取出Pager对象
            {
                commodityPager = (Pager) request.getSession().getAttribute(
                        "commodityPager");
                commodityPager.checkRequest(request);
                request.getSession().setAttribute("commodityPager", commodityPager);
            }
            else//否则就把用管理类中的方法重新获得一个Pager对象
            {
                commodityPager = commodityTypeManager.getComPager1(request,
                        commodityTypeForm);
                request.getSession().setAttribute("commodityPager", commodityPager);
            }


//根据获得的Pager对象,查询出要显示的记录(list)

           commodityResultList = commodityTypeManager.getComTypeListPage(
                    (CommodityTypeForm) (commodityPager.getForm()),                  commodityPager.getPageSize(),
                    commodityPager.getStartRow());

       //页面要用的信息

       request.setAttribute("PAGER", commodityPager);
        request.setAttribute("commodityresultlist", commodityResultList);

4.开始写自定义标签,其主要是显示上一页,下一页等操作,而要输出的list是在页面上用<logic:iteator>l输出到页面的,书写自定义标签的步骤如下:

(1)创建标签处理类(ChengPage):

package com.neusoft.training.common;

import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

import com.neusoft.training.database.oe.Pager;

import java.io.*;

public class ChengPage extends TagSupport
{

    /**
     * <p>Discription:[字段功能描述]</p>
     */
    private static final long serialVersionUID = 1L;

    String url;

    Pager pager;

//自定义标签的两个属性

    public String getUrl()
    {
        return url;
    }

    public void setUrl(String url)
    {
        this.url = url;
    }

    public Pager getPager()
    {
        return pager;
    }

    public void setPager(Pager pager)
    {
        this.pager = pager;
    }

    public int doStartTag() throws JspException
    {
        try
        {
            String pageString = "";
            if (this.getPager() != null)
            {
                if (pager.getCurrentPage() != 1)
                {
                    pageString += "<a href=/""
                            + url
                            + ".do?method=doQuery&pageMethod=first"
                            + "/"><img src=/"../images/fir.bmp/" alt=/"首页/"></a>";

//.do?method=doQuery&pageMethod=first等要根据自己的路径填写
//带<img>的等要根据自己的路径填写

                   pageString += "<a href=/""
                            + url
                            + ".do?method=doQuery&pageMethod=previous"
                            + "/"><img src=/"../images/pre.bmp/" alt=/"上一页/"></a>";
                    if (pager.getCurrentPage() == pager.getTotalPages())
                    {
                        pageString += "<img src=/"../images/next2.bmp/" alt=/"下一页/">";
                        pageString += "<img src=/"../images/last2.bmp/" alt=/"尾页/">";
                    }
                    else
                    {
                        pageString += "<a href=/""
                                + url
                                + ".do?method=doQuery&pageMethod=next"
                                + "/"><img src=/"../images/next.bmp/" alt=/"下一页/"></a>";
                        pageString += "<a href=/""
                                + url
                                + ".do?method=doQuery&pageMethod=last"
                                + "/"><img src=/"../images/last.bmp/" alt=/"尾页/"></a>";
                    }
                }
                else
                {
                    pageString += "<img src=/"../images/fir2.bmp/" alt=/"首页/">";
                    pageString += "<img src=/"../images/pre2.bmp/" alt=/"上一页/">";
                    if (pager.getCurrentPage() == pager.getTotalPages())
                    {
                        pageString += "<img src=/"../images/next2.bmp/" alt=/"下一页/">";
                        pageString += "<img src=/"../images/last2.bmp/" alt=/"尾页/">";
                    }
                    else
                    {
                        pageString += "<a href=/""
                                + url
                                + ".do?method=doQuery&pageMethod=next"
                                + "/"><img src=/"../images/next.bmp/" alt=/"下一页/"></a>";
                        pageString += "<a href=/""
                                + url
                                + ".do?method=doQuery&pageMethod=last"
                                + "/"><img src=/"../images/last.bmp/" alt=/"尾页/"></a>";
                    }
                }

                pageString += "<img src=/"../images/go.png/" alt=/"goto/""
                        + "οnclick=/"gotopage('pagenum','"
                        + url
                        + "')/">"
                        + "<input type=/"text/" name=/"pagenum/" id=/"pagenum/""
                        + "style=/"width: 20px/" value=/""
                        + pager.getCurrentPage() + "/" />/"
                        + pager.getTotalPages() + "页&nbsp;&nbsp;";
                pageString += "共" + pager.getTotalRows() + "条记录&nbsp;&nbsp;";
                pageString += "当前显示第" + (pager.getStartRow() + 1) + "条到";

                if ((pager.getStartRow() + 10) <= pager.getTotalRows())
                {
                    pageString += pager.getStartRow() + 10;
                }
                else
                {
                    pageString += pager.getTotalRows();
                }
                pageString += "条记录";
            }
            pageContext.getOut().write(pageString);
        }
        catch (IOException e)
        {
            System.out.println("Page Tag Error :" + e);
        }
        return super.doStartTag();
    }
}
(2)配置自定义标签,先在WEB-INF下建一个ctj-page.tld,其内容如下:

<?xml version="1.0" encoding="UTF-8" ?>
<taglib 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"
     version="2.0">
   
     <description>My Taglib by JavaWorld.com.tw</description>
     <tlib-version>1.0</tlib-version>
     <jsp-version>2.0</jsp-version>
     <short-name>PageTest</short-name>
     <uri></uri>
     <small-icon></small-icon>
   
     <tag>
       <description>Example:Hello</description>
       <name>page</name>
       <tag-class>com.neusoft.training.common.ChengPage</tag-class>
       <body-content>empty</body-content>
      <attribute>
         <name>url</name>
         <required>false</required>
         <rtexprvalue>true</rtexprvalue>
      </attribute>
      <attribute>
         <name>pager</name>
         <required>false</required>
         <rtexprvalue>true</rtexprvalue>
      </attribute>
    </tag>
</taglib>

(3)在页面上引用自定义标签:

<%@ taglib uri="/WEB-INF/ctj-page.tld" prefix="ctj" %>

 

<logic:iterate id="equchange" name="changeresultlist">

  //这里输出查询到的记录

</logic:iterate>

//显示上一页、下一页等

<ctj:page  url="/OASystem1/equipmentchange" pager="${PAGER}"/>


(4)效果如下:

  ①初始页面如下:

初始页面

②点击“查询”按钮后的页面如下:

点击“查询”后的页面

③点击“下一页”后的页面:

点击“下一页”后的效果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值