`

JSP自定义分页标签(struts1 和struts2等均适用)

    博客分类:
  • JSP
阅读更多

(struts1struts2 均适用,不依赖任何框架)

一.创建dream.tld 放在WEB-INF

<?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>A tag library exercising SimpleTag handlers.</description>
    <tlib-version>1.0</tlib-version>
    <short-name>dream</short-name>
    <uri>/dream</uri>
    <description>version 1.0</description>
    <tag>
        <name>page</name>
        <tag-class>com.taglib.PageTag



</tag-class>
        <body-content>empty</body-content>
        <attribute>
            <!--标签中的属性-->
            <name>name</name>
            <!--属性名-->
            <required>true</required>
            <!--是否必须-->
            <rtexprvalue>true</rtexprvalue>
            <!--表示该自定义标签的某属性的值可以直接指定或者通过动态计算指定-->
        </attribute>
        <attribute>
            <name>url</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>styleClass</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
</taglib>

 

 

二.创建Page.java(有点乱)

 

public class Page {

  /** 当前页 */
    private int pageNo;

    /** 每页数据量 */
    private int pageSize;

    /** 总数据量 */
    private int allCount;

    /** 总页数 */
    private int allPage;

    /** 每页开始 */
    private int recordStart;

    /** 每页结束 */
    private int recordEnd;

    /** 是否有上一页 */
    private boolean hasPrePage;

    /** 是否有下一页 */
    private boolean hasNextPage;

    /** 显示页数 */
    private int showCount;

    /** 显示开始 */
    private int showStart;

    /** 显示结束 */
    private int showEnd;


   // set get方法略

    public Page() {
        pageNo = 1;
        pageSize = 0;
        allPage = 1;
        recordStart = 0;
        recordEnd = 0;
        hasPrePage = false;
        hasNextPage = false;
        try {
            excecute();
        } catch (Exception exception) {
        }
    }

    public Page(int pageNo, int allCount) {
        this.pageNo = pageNo;
        this.pageSize = 25;
        this.allCount = allCount;
        this.allPage = 1;
        this.recordStart = 0;
        this.recordEnd = 0;
        hasPrePage = false;
        hasNextPage = false;
        try {
            excecute();
        } catch (Exception exception) {
        }
    }

    public Page(int pageNo, int pageSize, int allCount) {
        this.pageNo = pageNo;
        this.pageSize = pageSize;
        this.allCount = allCount;
        this.allPage = 1;
        this.recordStart = 0;
        this.recordEnd = 0;
        hasPrePage = false;
        hasNextPage = false;
        try {
            excecute();
        } catch (Exception exception) {
        }
    }

    public void excecute() {
        if (pageNo <= 0) {
            pageNo = 1;
        }
        recordStart = (pageNo - 1) * pageSize + 1;
        recordEnd = Math.min(recordStart + pageSize, allCount);
        if (allCount % pageSize == 0) {
            allPage = allCount / pageSize;
        } else {
            allPage = allCount / pageSize + 1;
        }
        if (pageNo > 1) {
            hasPrePage = true;
        }
        if (pageNo < allPage) {
            hasNextPage = true;
        }
        if (showCount <= 0) {
            showCount = 9;
        }

      //设定显示N页,我设定为9个;看效果图
        showCount = Math.min(showCount, allPage);
        if (showCount < 9) {
            showStart = 1;
            showEnd = showCount + showStart;
        } else {
            if (pageNo - 3 <= 1) {
                showStart = 1;
            } else {
                showStart = pageNo - 3;
            }
            if (allPage - 3 < pageNo) {
                showEnd = allPage - 3;
            } else {
                if (showStart == 1) {
                    showEnd = showStart + 6;
                } else {
                    showEnd = pageNo + 3;
                }
            }
        }
       }
}

 

三.创建PageTag.java(没有具体研究,返回值,先用0代替吧)

 

public class PageTag extends BodyTagSupport {

    private String name;

    private String url;

    private String styleClass;

   // set get方法略

    public int doEndTag() throws JspException {
        try {
            HttpServletRequest request = (HttpServletRequest) super.pageContext.getRequest();
            Object obj = request.getAttribute("pageInfo");
            if (obj == null) {
                return 0;
            }
            if (!(obj instanceof Page)) {
                return 0;
            }
            Page page = (Page) obj;
            StringBuffer sb = new StringBuffer();

            //可根据page的get方法设定内容,我的如下:
            sb.append("<input type='button' value='首页'" + getReadOnly(page.isHasPrePage()) + "/>&nbsp;");
            sb.append("<input type='button' value='上一页'" + getReadOnly(page.isHasPrePage()) + "/>&nbsp;");
            if (page.getShowStart() > 1) {
                sb.append("...&nbsp;");
            }
            for (int i = page.getShowStart(); i < page.getPageNo(); i++) {
                sb.append("<a href='#'>" + i + "</a>&nbsp;");
            }
            sb.append("<a href='#' class='" + styleClass + "'>" + page.getPageNo() + "</a>&nbsp;");
            for (int i = page.getPageNo() + 1; i <= page.getShowEnd(); i++) {
                sb.append("<a href='#'>" + i + "</a>&nbsp;");
            }
            if (page.getShowEnd() < page.getAllPage()) {
                sb.append("...&nbsp;");
            }
            sb.append("<input type='button' value='下一页'" + getReadOnly(page.isHasNextPage()) + "/>&nbsp;");
            sb.append("<input type='button' value='尾页'" + getReadOnly(page.isHasNextPage()) + "/>");



            JspWriter jspwriter = super.pageContext.getOut();
            jspwriter.write(sb.toString());
        } catch (Exception exception) {
            throw new JspException(exception);
        }
        return 0;
    }
    private String getReadOnly(boolean b) {
		if (!b) {
			return "class='disableButton'";
		}
		return "class='button'";
   }

 

四.

public class XXAction extends MainAction {

      /**页面的数据结果*/

      private List result;

      private Page pageInfo;

      private String url;

      /** 当前页*/

      private int page;

      //get Set略

     //其他略

     protected String execute() throws Exception {
            List data = new ArrayList();
            for (int i = 0; i < 1000; i++) {

                //自行定地XXEntity ,就是一个javaBean,测试用
                XXEntity entity = new XXEntity ();
                entity.setID(i % 2);

                ....
                data.add(entity);
            }

            //必须有这几步
            pageInfo = new Page(page, data.size());
            pageInfo.excecute();
            result = data.subList(pageInfo.getRecordStart() - 1, pageInfo.getRecordEnd() - 1);
            url="...";
        return SUCCESS;
    }

}

 

 

五.jsp

 

<%@ page contentType="text/html;charset=utf-8"%>

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

    ...

    <dream:page name="pageInfo" url="url" styleClass="currentA" />

    ...

 

 

六.效果图

分页

七,css,可能不完备,需要body式样等支持, 式样可根据个人喜好了

.button {
	background-color:#F3FAFF;
	border: 1px solid #7F9DB9;
	padding-right: 7px;
	padding-left: 7px;
	padding-top: 1px;
	cursor:pointer;
	color:#005BAC;
}
.button:hover{
	padding-right: 7px;
	padding-left: 7px;
	padding-top: 1px;
	background-color:#005BAC;
	color:#F3FAFF;
}
.disableButton{
	background-color:#F3FAFF;
	border: 1px solid #7F9DB9;
	padding-right: 7px;
	padding-left: 7px;
	padding-top: 1px;
	color:#005BAC;
}
a {
	color: #005EAC
}

a:link {
	color: #005EAC;
	text-decoration: none
}
a:visited{
	text-decoration: none
}

a:hover {
	color: #C00;
	text-decoration: underline
}
....

 

 

  • 大小: 5.8 KB
3
0
分享到:
评论
3 楼 vern_jang 2012-07-03  
你这个是strut2的吧,strut1怎么用,不好使,可以教一下吗?
2 楼 xiaojianhx 2009-12-02  
zoudx 写道
第三点.创建PageTag.java里面

page.getShowStart()方法找不到



在"二.创建Page.java"里,有showStart属性,
添加get set方法就可以了
1 楼 zoudx 2009-12-01  
第三点.创建PageTag.java里面

page.getShowStart()方法找不到

相关推荐

Global site tag (gtag.js) - Google Analytics