您的位置:首页 > 编程语言 > Java开发

java web 分页技术总结

2015-06-01 10:13 603 查看
最近再做一个简易BBS,其中要用到分页技术,以前做过一个小型网上书店系统,记得当时是用弄了一个接口:PagingInterface,其中有得到总记录数、得到总页数、和一些根据当前页到结束页的查询方法。  然后再一个UserDAO类、BookDAO类中继承Paging方法,实现相应的“一些根据当前页到结束页的查询方法”等,然后再一个PagingServlet类中调用这些方法。。。。具体的类代码这里也不贴了,这种方法复用性不高,结构性差,不推荐使用。这次做这个BBS,经过几次的调试,终于弄成功了。下面是分页的主类:

[html] view
plaincopy

/**  

 * 分页对象,进行一系列分页操作  

 *   

 * @author weiyiorng  

[html] view
plaincopy

 * @version 1.0 12/03/20  

 * */  

public class SplitPage {  

    // 声明一些常量  

    final public static String FIRSTPAGE = "first";// 请求的是首页  

    final public static String PREVIOUSPAGE = "previous";// 请求上一页  

    final public static String NEXTPAGE = "next";// 请求下一页  

    final public static String LASTPAGE = "last";// 请求最后一页  

  

    // 声明一些变量  

    private int pageRow = 3;// 每页显示记录数  

    private int totalRow = 0;// 总的记录数,有数据库操作类DAO提供  

    private int currentPage = 1;// 当前的页面  

    private int firstPage = 1;// 首页位置  

    private int totalPage = 1;// 总的页面数,默认为一页  

  

    public int getPageRow() {  

        return pageRow;  

    }  

  

    /**  

     * 重新设置每页显示的记录数  

     *   

     * @param pageRow  

     *            新的每页显示记录数  

     */  

    public void setPageRow(int pageRow) {  

        if (pageRow == 0) {  

            try {  

                throw new Exception();// 如果pageRow被设置为零,应当抛出异常.  

            } catch (Exception e) {  

                // TODO Auto-generated catch block  

                e.printStackTrace();  

            }  

        }  

  

        this.pageRow = pageRow;// 修改每页的记录数  

        this.totalPage = this.totalRow / this.pageRow  

                + ((this.totalRow % this.pageRow == 0) ? 0 : 1);  

    }  

  

    public int getTotalRow() {  

        return totalRow;  

    }  

  

    public void setTotalRow(int totalRow) {// 设置分页对象的总记录属性后,就应该根据每页面显示记录数,计算得到总的页面数  

        this.totalRow = totalRow;  

        this.totalPage = this.totalRow/this.pageRow+((this.totalRow%this.pageRow==0)?0:1);  

        System.out.println("当前页"+this.currentPage);  

  

    }  

  

    public int getCurrentPage() {  

        return currentPage;  

    }  

  

    public void setCurrentPage(int currentPage) {  

        this.currentPage = currentPage;  

    }  

  

    public int getFirstPage() {  

        return firstPage;  

    }  

  

    public void setFirstPage(int firstPage) {  

        this.firstPage = firstPage;  

    }  

  

    public int getTotalPage() {  

        return totalPage;  

    }  

    //不应该提供方法设置总页面数,它是计算得到的   

  

    /**  

     * 根据请求的标示符参数重新计算要现实的页面  

     *   

     * @param flag  

     *            请求转向的页面标示符  

     * @return int 返回新页  

     */  

    public int toNewPage(String flag) {  

        int newPage = this.currentPage;  

        if (flag != null && !"".equals(flag)) {  

            if (SplitPage.FIRSTPAGE.equals(flag)) {  

                newPage = 1;  

            } else if (SplitPage.LASTPAGE.equals(flag)) {  

                newPage = this.totalPage;  

            } else if (SplitPage.NEXTPAGE.equals(flag)) {  

                newPage = this.currentPage  

                        + ((this.currentPage == this.totalPage) ? 0 : 1);// 如果当前页面与总的页面数相等则不再向后(+1)  

            } else if (SplitPage.PREVIOUSPAGE.equals(flag)) {  

                newPage = this.currentPage  

                        - ((this.currentPage == this.firstPage) ? 0 : 1);// 如果当前页面与首页相等则不再向前(-1)  

            } else {  

                // 传入的是个数字字符串参数  

                newPage = Integer.parseInt(flag.trim());  

            }  

        } else {// 请求的参数为空,则当前页码不变  

            newPage = this.currentPage;  

        }  

        this.setCurrentPage(newPage);// 记得重新设置当期页面  

        return newPage;  

    }  

}  

为什么说这个类重要呢?因为你在想要分页的时候,所有的分页参数都可以在这里面设置,动态静态的都可以实现,复用性很高!

下面声明一个分页接口,设置具体的分页对象(比如你要分页的是用户列表还是帖子列表)等:

[java] view
plaincopy

public interface PageInterface {  

    /** 

     * 查询所有的记录,调用分页生成器类中的分页方法查询数据 

     *  

     * @param splitPage 

     *            分页对象 

     * @return List<Object> 

     * */  

    public List<UserInfoVo> findUserAll(SplitPage splitPage);//查询用户列表  

    /**  

     * 查询所有的记录,调用分页生成器类中的分页方法查询数据  

     *   

     * @param splitPage  

[java] view
plaincopy

           *            分页对象  

     * @return List<Object>  

     * */  

    public List<ForumInfoVo> findForumAll(SplitPage splitPage);//查询帖子  

    /** 

     * 提供总的记录数 

     * */  

  

    public int getTotalRows();  

    /** 

     * 查询所有的记录,调用分页生成器类中的分页方法查询数据 

     *  

     * @param splitPage 

     *            分页对象 

     * @return List<Object> 

     * */  

    public List<ReforumInfoVo> findReforumAll(SplitPage splitPage);  

}  

然后在各个DAO类(用户DAO、帖子DAO、RefoumDAO等)中实现此接口相应方法,注意在各个DAO类中应该有一个totalRow总记录数的获取和设置方法,得到指定记录数方法,简单起见,我这里是直接得到所有记录getAllRecord()(可以再分页显示时调用以设置分页类别,如查询某一个昵称的所有用户等)。

接下咱废话少说,直接上显示分页效果的代码:

forumList.jsp

[java] view
plaincopy

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  

<%@taglib prefix="c" uri="http://java.sun.com/jsf/core"%>  

<%@page import="com.weiyi.bbs.dao.ForumInfoDAO"%>  

<%@page import="com.weiyi.bbs.util.*"%>  

<%@page import="com.weiyi.bbs.domain.*"%>  

<%  

    String path = request.getContextPath();  

    String basePath = request.getScheme() + "://"  

            + request.getServerName() + ":" + request.getServerPort()  

            + path + "/";  

%>  

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  

<html>  

    <head>  

        <base href="<%=basePath%>">  

  

        <title>帖子列表</title>  

  

        <meta http-equiv="pragma" content="no-cache">  

        <meta http-equiv="cache-control" content="no-cache">  

        <meta http-equiv="expires" content="0">  

        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  

        <meta http-equiv="description" content="This is my page">  

        <script type="text/javascript" src="js/common.js" charset="utf-8"></script>  

        <script type="text/javascript" charset="utf-8">  

        function goPage(){  

           var v = document.getElementById("selectPage").value;  

           var u = document.getElementById("userId").value;  

           window.location.href="forumList.jsp?flag="+v+"&userId="+u;  

        }  

        </script>  

    </head>  

    <!--spage分页对象,保存分页的详细信息 ,存在Session中,每次查询或者是分页数据时只要设置此对象的当前页 -->  

    <!-- dao帖子操作类,主要用来获取总记录数-->  

    <jsp:useBean id="spage" class="com.weiyi.bbs.util.SplitPage"  

        scope="session"></jsp:useBean>  

    <jsp:useBean id="dao" class="com.weiyi.bbs.dao.ForumInfoDAO"  

        scope="session"></jsp:useBean>  

    <%  

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

        int newPage = spage.toNewPage(flag.trim());  

        dao.setTotalRows(dao.getAllRecord());  

        int totalRows = dao.getTotalRows();  

        spage.setTotalRow(totalRows);  

    %>  

    <body>  

        <div id="wrap">  

            <h3>  

                查看帖子列表  

            </h3>  

            <input type="hidden" name="userId" id="userId" value="<%=request.getParameter("userId")%>"/>  

            <a href="destroyservlet">退出</a>     

            <a  

                href="forumcontroller?type=toNewForum&userId=<%=request.getParameter("userId")%>&power=user">发新帖</a>  

            <table border="border">  

                <thead>  

                    <tr>  

                        <th>  

                            ID  

                        </th>  

                        <th>  

                            标题  

                        </th>  

                        <th>  

                            发帖人  

                        </th>  

                        <th>  

                            发帖时间  

                        </th>  

                        <th>  

                            操作  

                        </th>  

                    </tr>  

                </thead>  

                <tbody>  

                    <%  

                        List<ForumInfoVo> list = dao.findForumAll(spage);  

                        for (ForumInfoVo forum : list) {  

                    %>  

                    <tr>  

                        <td><%=forum.getId()%></td>  

                        <td><%=forum.getTitle()%></td>  

                        <td><%=forum.getAuthorName()%></td>  

                        <td><%=forum.getCreateTime()%></td>  

                        <td>  

                            <a  

                                href="forumcontroller?type=look&power=user&userId=<%=request.getParameter("userId")%>&id=<%=forum.getId()%>">查看</a>    

                        </td>  

                    </tr>  

                    <%  

                        }  

                    %>  

                </tbody>  

            </table>  

            <div id="foot">  

                <a href="forumList.jsp?flag=<%=SplitPage.FIRSTPAGE%>&userId=<%=request.getParameter("userId")%>">首页</a>   

                <a href="forumList.jsp?flag=<%=SplitPage.PREVIOUSPAGE%>&userId=<%=request.getParameter("userId")%>">上一页</a>   

                <a href="forumList.jsp?flag=<%=SplitPage.NEXTPAGE%>&userId=<%=request.getParameter("userId")%>">下一页</a>   

                <a href="forumList.jsp?flag=<%=SplitPage.LASTPAGE%>&userId=<%=request.getParameter("userId")%>">末页</a>   

                <select id="selectPage" name="selectPage" onchange="goPage();">  

                    <%  

                        for (int i = 1; i <= spage.getTotalPage(); ++i) {  

                    %>  

                    <option value="<%=i%>"  

                        <%=(spage.getCurrentPage() == i) ? "selected='selected'"  

                        : ""%> ><%=i%></option>  

                        <%  

                            }  

                        %>  

                      

                </select>  

                <label><%=spage.getCurrentPage()%>/<%=spage.getTotalPage()%>页  

                </label>  

            </div>  

        </div>  

    </body>  

</html>  

 

 运行效果:



有什么地方有不足之处,往各位同行多多指教哈!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: