基于mySql的JAVA分页自定义标签详解

来源:互联网 发布:c语言汉字变量 编辑:程序博客网 时间:2024/05/19 14:20

第一步、建立一个分页类:

package com.test.freamwork.page;

import java.util.List;

/**
 * 列表分页。包含list属性。
 *
 *
 */
public class Pagination{


    public static final int DEF_COUNT = 20;
    protected int totalCount = 0;//总条数
    protected int pageSize = 20;//每页多少条
    protected int pageNo = 1;//当前页码
    private List<?> list;
   
    public Pagination() {
    }
   
    /**
     * 构造器
     *
     * @param pageNo
     *            页码
     * @param pageSize
     *            每页几条数据
     * @param totalCount
     *            总共几条数据
     * @param list
     *            分页内容
     */
    public Pagination(int pageNo, int pageSize, int totalCount, List<?> list) {
        setTotalCount(totalCount);
        setPageSize(pageSize);
        setPageNo(pageNo);
        adjustPageNo();
        this.list = list;
    }
   
    /**
     * 构造器
     *
     * @param pageNo
     *            页码
     * @param pageSize
     *            每页几条数据
     * @param totalCount
     *            总共几条数据
     */
    public Pagination(int pageNo, int pageSize, int totalCount) {
        setTotalCount(totalCount);
        setPageSize(pageSize);
        setPageNo(pageNo);
        adjustPageNo();
    }

    /**
     * 调整页码,使不超过最大页数
     */
    public void adjustPageNo() {
        if (pageNo == 1) {
            return;
        }
        int tp = getTotalPage();
        if (pageNo > tp) {
            pageNo = tp;
        }
    }

    /**
     * 获得页码
     */
    public int getPageNo() {
        return pageNo;
    }

    /**
     * 每页几条数据
     */
    public int getPageSize() {
        return pageSize;
    }

    /**
     * 总共几条数据
     */
    public int getTotalCount() {
        return totalCount;
    }

    /**
     * 总共几页
     */
    public int getTotalPage() {
        int totalPage = totalCount / pageSize;
        if (totalPage == 0 || totalCount % pageSize != 0) {
            totalPage++;
        }
        return totalPage;
    }

    /**
     * 是否第一页
     */
    public boolean isFirstPage() {
        return pageNo <= 1;
    }

    /**
     * 是否最后一页
     */
    public boolean isLastPage() {
        return pageNo >= getTotalPage();
    }

    /**
     * 下一页页码
     */
    public int getNextPage() {
        if (isLastPage()) {
            return pageNo;
        } else {
            return pageNo + 1;
        }
    }

    /**
     * 上一页页码
     */
    public int getPrePage() {
        if (isFirstPage()) {
            return pageNo;
        } else {
            return pageNo - 1;
        }
    }

    /**
     * if totalCount<0 then totalCount=0
     *
     * @param totalCount
     */
    public void setTotalCount(int totalCount) {
        if (totalCount < 0) {
            this.totalCount = 0;
        } else {
            this.totalCount = totalCount;
        }
    }

    /**
     * if pageSize< 1 then pageSize=DEF_COUNT
     *
     * @param pageSize
     */
    public void setPageSize(int pageSize) {
        if (pageSize < 1) {
            this.pageSize = DEF_COUNT;
        } else {
            this.pageSize = pageSize;
        }
    }

    /**
     * if pageNo < 1 then pageNo=1
     *
     * @param pageNo
     */
    public void setPageNo(int pageNo) {
        if (pageNo < 1) {
            this.pageNo = 1;
        } else {
            this.pageNo = pageNo;
        }
    }

 /**
  * 第一条数据位置
  *
  * @return
  */
 public int getFirstResult() {
  return (pageNo - 1) * pageSize;
 }

 /**
  * 获得分页内容
  *
  * @return
  */
 public List<?> getList() {
  return list;
 }

 /**
  * 设置分页内容
  *
  * @param list
  */
 public void setList(List list) {
  this.list = list;
 }
}

第二步、分页标签类:

package com.test.freamwork.PageTag;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.test.freamwork.page.Pagination;

@SuppressWarnings("serial")
public class PageTag extends TagSupport{

 private String pageName;
 private String uri;
 
 @Override
 public int doStartTag() throws JspException {

  JspWriter out = this.pageContext.getOut();
  HttpServletRequest request = (HttpServletRequest) this.pageContext.getRequest();
  try {
      Pagination page = (Pagination)request.getAttribute( pageName );
   int pageNo = page.getPageNo();//第几页
   int pageSize = page.getPageSize();//第页多少条
   int rowCount = page.getTotalCount();//总条数
   int pageCount = page.getTotalPage();//总页数

   out.println("<table width=\"100%\">");
   out.println(" <tr>");
   out.println("  <td align=\"right\">");
   out.println("  [总数:"+rowCount+"]&nbsp;&nbsp;");

   if(pageNo >1){
    out.println(" <a href=\""+request.getContextPath()+uri+"?pageNo=1\">[首页]</a>&nbsp;&nbsp;");
    out.println(" <a href=\""+request.getContextPath()+uri+"?pageNo="+(pageNo-1)+"\">[上一页]</a>&nbsp;&nbsp;");
   }else{  
    out.println(" [首页]&nbsp;&nbsp;");
    out.println(" [上一页]&nbsp;&nbsp;");
   }
  
   if( pageNo < pageCount ){
    out.println(" <a href=\""+request.getContextPath()+uri+"?pageNo="+(pageNo+1)+"\">[下一页]</a>&nbsp;&nbsp;");
    out.println(" <a href=\""+request.getContextPath()+uri+"?pageNo="+pageCount+"\">[尾页]</a>  &nbsp;&nbsp;");
   }else{
    out.println(" [下一页]&nbsp;&nbsp;");
    out.println(" [尾页]&nbsp;&nbsp;");
   }
     
   out.println(" 每页显示"+pageSize+"   条 ");
   out.println(" </td>");
   out.println("</tr>");
   out.println("</table>");
   
  } catch (IOException e) {
   e.printStackTrace();
  }
  return super.doStartTag();
 }

 public String getPageName() {
  return pageName;
 }

 public void setPageName(String pageName) {
  this.pageName = pageName;
 }

 public String getUri() {
  return uri;
 }

 public void setUri(String uri) {
  this.uri = uri;
 }

 
}

第三步、分页tld编写,放在WEB-INF下,命名为page.tld

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>bean</shortname>

<tag>
    <name>page</name>
    <tagclass>com.test.freamwork.PageTag.PageTag</tagclass>
   
    <attribute>
     <name>uri</name>
     <required>true</required>
     <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
     <name>pageName</name>
     <required>true</required>
     <rtexprvalue>false</rtexprvalue>
    </attribute>
</tag>

<tag>
    <name>page2</name>
    <tagclass>com.test.freamwork.PageTag.PageTag2</tagclass>
   
    <attribute>
        <name>uri</name>
        <required>true</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <name>pageName</name>
        <required>true</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
</tag>

<tag>
    <name>page3</name>
    <tagclass>com.test.freamwork.PageTag.PageTag3</tagclass>
   
    <attribute>
        <name>uri</name>
        <required>true</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <name>pageName</name>
        <required>true</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
</tag>

</taglib>

第五步、servelt写法

package com.test.web;

import j.u.StringUtil;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.test.dat.ExampleDB;
import com.test.freamwork.page.Pagination;

/** 
 * Filename:    WjpController.java 
 * Description:  
 * Copyright:   Copyright (c)2013
 * Company:     yunMall
 * @author:     Mr-Wu 
 * @version:    1.0 
 * Create at:   2013-9-23 上午10:07:31 
 * 
 * Modification History: 
 * Date         Author      Version     Description 
 * ------------------------------------------------------------------ 
 * 2013-9-23      Mr-Wu        1.0        1.0 Version 
 */
public class WjpController extends BasePage{

    private static final long serialVersionUID = 1L;
   
   
    public void processRequest(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        List<Object> list = new ArrayList<Object>();
        Pagination page = new Pagination();
        if (StringUtil.isNotBlank(request.getParameter("pageNo")))
            page.setPageNo(new Integer(request.getParameter("pageNo")));
        if (StringUtil.isNotBlank(request.getParameter("pageSize")))
            page.setPageSize(new Integer(request.getParameter("pageSize")));
        page.setTotalCount(ExampleDB.selectCount());
        page.adjustPageNo();
        list = ExampleDB.selectUserList(page.getFirstResult(),
                page.getPageSize() + 1);
        request.setAttribute("page", page);
        request.setAttribute("list", list);
    }

}

第六步、JSP引用说明

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" extends="com.test.web.WjpController"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib uri="/WEB-INF/page.tld" prefix="p"%>
<c:set var="basePath" value="${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}${pageContext.request.contextPath}"></c:set>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

 <table class=editTable cellSpacing=1 cellPadding=0 width="100%" align=center border=0>
     <tr bgcolor="#FFFDF0" align="center">
        <td ><div align="center">id</div></td>
         <td ><div align="center">name</div></td>
         <td ><div align="center">age</div></td>
         <td ><div align="center">phone</div></td>
         <td ><div align="center">password</div></td>
     <c:forEach items="${list}" var="stu">
         <tr bgcolor="#ECF3FD" class=editHeaderTr align="center">
             <td>${stu.uid}</td>
             <td>${stu.name}</td>
             <td>${stu.age}</td>
             <td>${stu.phone}</td>
             <td>${stu.passwd}</td>
         </tr>
     </c:forEach>
 </table>
<table class=editTable cellSpacing=1 cellPadding=0 width="100%" align=center border=0>
  <tr bgcolor="#ECF3FD">
    <td><div align="left"><p:page2 uri="/sampleJava/wjp/wjp.j"  pageName="page"/></div></td>
  </tr>
</table>
</body>
</html>

 

更多分页样式,有待更新。

原创粉丝点击