`
zhangfeilo
  • 浏览: 390779 次
  • 性别: Icon_minigender_1
  • 来自: 昆明
社区版块
存档分类
最新评论

jsp分页控件

    博客分类:
  • J2EE
阅读更多

1.PagingTag.java

 

package com.cn.cosoft.util.taglibs;

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

/** */
/**
 * 自定义的分页标签
 *
 * @author 马万林
 *
 */
public class PagingTag extends TagSupport {

    private String url = null;
    private int pageIndex;
    private int pageMax;

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

    public String getUrl() {
        return this.url;
    }

    public void setPageIndex(int pageIndex) {
        this.pageIndex = pageIndex;
    }

    public int getPageIndex() {
        return this.pageIndex;
    }

    public void setPageMax(int pageMax) {
        this.pageMax = pageMax;
    }

    public int getPageMax() {
        return this.pageMax;
    }


    @Override
    public int doStartTag() throws JspException {
        String str = "";
        if (pageIndex == 1) {
            str += "首页 上一页 ";
        } else {
            str += " <a href='" + url + "pageIndex=1'>首页</a> "
                    + "<a href='" + url + "pageIndex=" + (pageIndex - 1) + "'>上一页</a> ";
        }
        if (pageIndex / 6 < 1.0 || pageMax < 10) {
            for (int i = 1; i <= 9; i++) {
                if (i <= pageMax) {
                    if (pageIndex != i) {
                        str += "<a href='" + url + "pageIndex=" + i + "'>[" + i + "]</a> ";
                    } else {
                        str += "  " + i + " ";
                    }
                }
            }
        } else if (pageIndex / 6 >= 1.0 && pageMax >= 10) {
            int fri = 0;
            int max = 0;
            if (pageMax - pageIndex > 4) {
                fri = pageIndex - 4;
                max = pageIndex + 4;
            } else {
                fri = pageMax - 8;
                max = pageMax;
            }
            for (int i = fri; i <= max; i++) {
                if (i <= pageMax) {
                    if (pageIndex != i) {
                        str += "<a href='" + url + "pageIndex=" + i + "'>[" + i + "]</a> ";
                    } else {
                        str += "  " + i + " ";
                    }
                }
            }
        }
        if (pageIndex == pageMax || pageMax < 2) {
            str += "下一页 尾页";
        } else {
            str += "<a href='" + url + "pageIndex=" + (pageIndex + 1) + "'>下一页</a> "
                    + "<a href='" + url + "pageIndex=" + pageMax + "'>尾页</a>";
        }
        try {
            if (str != "") {
                pageContext.getOut().write(str);
            }
        } catch (Exception e) {
            throw new JspException(e);
        }
        return EVAL_PAGE;
    }
}

 2.pagingTag.tld

 

<?xml  version="1.0"  encoding="utf-8"  ?>
 <!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>pagingTag</short-name>
    <!--OutputTag-->
    <tag>
        <name>pageOut</name>
        <tag-class>com.cn.cosoft.util.taglibs.PagingTag</tag-class>
        <body-content>empty</body-content>
        <attribute>
            <name>url</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>pageIndex</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>pageMax</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
</taglib>

 3.index.jsp

 

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="/WEB-INF/pagingTag.tld" prefix="pt"%>

<html>
    <body >

  <pt:pageOut url="当前页面路径?" pageIndex="当前分页页码"
                                pageMax="总的分页页码" ></pt:pageOut>

</body>

</html>
分享到:
评论
2 楼 Kattou 2014-10-15  
你好! 看了你写的这个分页 感觉很好, 但是不怎么会用么,请指教:页面是直接引用标签就可以使用了么?还是要怎么跟后台结合才可以使用?
1 楼 jspc 2011-11-26  
以前听说过,没看过,好的,记下了,学习  

相关推荐

Global site tag (gtag.js) - Google Analytics