jsp servlet 分页

Page:

jsp servlet  分页Page
public class Page {
    private int everyPage;            //每页显示记录数
    private int totalCount;            //总记录数
    private int totalPage;            //总页数
    private int currentPage;        //当前页
    private int beginIndex;            //查询起始点
    private boolean hasPrePage;        //是否有上一页
    private boolean hasNextPage;    //是否有下一页
    public Page(int everyPage, int totalCount, int totalPage, 
            int currentPage,int beginIndex, boolean hasPrePage,
            boolean hasNextPage) {    //自定义构造方法
        this.everyPage = everyPage;
        this.totalCount = totalCount;
        this.totalPage = totalPage;
        this.currentPage = currentPage;
        this.beginIndex = beginIndex;
        this.hasPrePage = hasPrePage;
        this.hasNextPage = hasNextPage;
    }
    public Page(){}                    //默认构造函数
    public int getEveryPage() {        //获得每页显示记录数
        return everyPage;
    }
    public void setEveryPage(int everyPage) {//设置每页显示记录数
        this.everyPage = everyPage;
    }
    public int getTotalCount() {//获得总记录数
        return totalCount;
    }
    public void setTotalCount(int totalCount) {//设置总记录数
        this.totalCount = totalCount;
    }
    public int getTotalPage() {//获得总页数
        return totalPage;
    }
    public void setTotalPage(int totalPage) {//设置总页数
        this.totalPage = totalPage;
    }
    public int getCurrentPage() {//获得当前页
        return currentPage;
    }
    public void setCurrentPage(int currentPage) {//设置当前页
        this.currentPage = currentPage;
    }
    public int getBeginIndex() {//获得查询起始点
        return beginIndex;
    }
    public void setBeginIndex(int beginIndex) {//设置查询起始点
        this.beginIndex = beginIndex;
    }
    public boolean isHasPrePage() {//获得是否有上一页
        return hasPrePage;
    }
    public void setHasPrePage(boolean hasPrePage) {//设置是否有上一页
        this.hasPrePage = hasPrePage;
    }
    public boolean isHasNextPage() {//获得是否有下一页
        return hasNextPage;
    }
    public void setHasNextPage(boolean hasNextPage) {//设置是否有下一页
        this.hasNextPage = hasNextPage;
    }
}

PageUtil:分页辅助类:

jsp servlet  分页
public class PageUtil {
    public static Page createPage(int everyPage,int totalCount,int currentPage) {//创建分页信息对象
        everyPage = getEveryPage(everyPage);
        currentPage = getCurrentPage(currentPage);
        int totalPage = getTotalPage(everyPage, totalCount);
        int beginIndex = getBeginIndex(everyPage, currentPage);
        boolean hasPrePage = getHasPrePage(currentPage);
        boolean hasNextPage = getHasNextPage(totalPage, currentPage);
        return new Page(everyPage, totalCount, totalPage, currentPage,
                beginIndex, hasPrePage,  hasNextPage);
    }
    public static int getEveryPage(int everyPage) {        //获得每页显示记录数
        return everyPage == 0 ? 10 : everyPage;
    }
    public static int getCurrentPage(int currentPage) {    //获得当前页
        return currentPage == 0 ? 1 : currentPage;
    }
    public static int getTotalPage(int everyPage,int totalCount) {//获得总页数
        int totalPage = 0;
        if(totalCount != 0 &&totalCount % everyPage == 0) {
            totalPage = totalCount / everyPage;
        } else {
            totalPage = totalCount / everyPage + 1;
        }
        return totalPage;
    }
    public static int getBeginIndex(int everyPage,int currentPage) {//获得起始位置
        return (currentPage - 1) * everyPage;
    }
    public static boolean getHasPrePage(int currentPage) {//获得是否有上一页
        return currentPage == 1 ? false : true;
    }
    public static boolean getHasNextPage(int totalPage, int currentPage) {    //获得是否有上一页
        return currentPage == totalPage || totalPage == 0 ? false : true;
    }
}
PageUtil

 

servlet中调用:

 

jsp servlet  分页
int currentPage = 0;
        String currentPageStr = request.getParameter("currentPage");
        if(currentPageStr == null || "".equals(currentPageStr)){
            currentPage = 1;
        }else {
            currentPage = Integer.parseInt(currentPageStr);
        }
        
        MessageDAO messageDAO = MessageDAOFactory.getMessageAOInstance();
        Page page = PageUtil.createPage(5, messageDAO.findAllCount(), currentPage);
        List<Message> messages = messageDAO.findAllMessagee(page);
        request.setAttribute("messageList", messages);
        request.setAttribute("page", page);
View Code

 

 

jsp servlet  分页JSP中:

jsp servlet  分页
<c:choose>
        <c:when test="${page.hasPrePage}">
            <a href="GetMessageList?currentPage=1">首页</a> | 
    <a href="GetMessageList?currentPage=${page.currentPage -1 }">上一页</a>
        </c:when>
        <c:otherwise>
            首页 | 上一页
        </c:otherwise>
    </c:choose>
    <c:choose>
        <c:when test="${page.hasNextPage}">
            <a href="GetMessageList?currentPage=${page.currentPage + 1 }">下一页</a> | 
    <a href="GetMessageList?currentPage=${page.totalPage }">尾页</a>
        </c:when>
        <c:otherwise>
            下一页 | 尾页
        </c:otherwise>
    </c:choose>
    当前为第${page.currentPage}页,共${page.totalPage}页
View Code

效果图:

jsp servlet  分页

jsp servlet 分页

上一篇:javascript函数重载


下一篇:网站安全漏洞说明(转)